java.lang.Object
g1801_1900.s1864_minimum_number_of_swaps_to_make_the_binary_string_alternating.Solution

public class Solution extends Object
1864 - Minimum Number of Swaps to Make the Binary String Alternating.<p>Medium</p> <p>Given a binary string <code>s</code>, return <em>the <strong>minimum</strong> number of character swaps to make it <strong>alternating</strong> , or</em> <code>-1</code> <em>if it is impossible.</em></p> <p>The string is called <strong>alternating</strong> if no two adjacent characters are equal. For example, the strings <code>&quot;010&quot;</code> and <code>&quot;1010&quot;</code> are alternating, while the string <code>&quot;0100&quot;</code> is not.</p> <p>Any two characters may be swapped, even if they are <strong>not adjacent</strong>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;111000&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> Swap positions 1 and 4: &ldquo;111000&rdquo; -> &ldquo;101010&rdquo; The string is now alternating.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;010&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The string is already alternating, no swaps are needed.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;1110&rdquo;</p> <p><strong>Output:</strong> -1</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minSwaps

      public int minSwaps(String s)