java.lang.Object
g2301_2400.s2380_time_needed_to_rearrange_a_binary_string.Solution

public class Solution extends Object
2380 - Time Needed to Rearrange a Binary String.<p>Medium</p> <p>You are given a binary string <code>s</code>. In one second, <strong>all</strong> occurrences of <code>&quot;01&quot;</code> are <strong>simultaneously</strong> replaced with <code>&quot;10&quot;</code>. This process <strong>repeats</strong> until no occurrences of <code>&quot;01&quot;</code> exist.</p> <p>Return <em>the number of seconds needed to complete this process.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;0110101&rdquo;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>After one second, s becomes &ldquo;1011010&rdquo;.</p> <p>After another second, s becomes &ldquo;1101100&rdquo;.</p> <p>After the third second, s becomes &ldquo;1110100&rdquo;.</p> <p>After the fourth second, s becomes &ldquo;1111000&rdquo;.</p> <p>No occurrence of &ldquo;01&rdquo; exists any longer, and the process needed 4 seconds to complete, so we return 4.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;11100&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> No occurrence of &ldquo;01&rdquo; exists in s, and the processes needed 0 seconds to complete, so we return 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 1000</code></li> <li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> </ul> <p><strong>Follow up:</strong></p> <p>Can you solve this problem in O(n) time complexity?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • secondsToRemoveOccurrences

      public int secondsToRemoveOccurrences(String s)