How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: ๐ก Medium
Topics: Greedy, Sliding Window, String
You are given a binary string s.
You are allowed to perform two types of operations in any sequence:
Remove the character at the start of the string and append it to the end.
Pick any character in s and flip its value:
'0' โ '1''1' โ '0'Return the minimum number of Type-2 (flip) operations required to make the string alternating.
A string is called alternating if no two adjacent characters are equal.
โ๏ธ Valid alternating strings:
"010""1010"โ Not alternating:
"0100"Input: s = "111000"
Output: 2
Explanation:
"100011""101010"Input: s = "010"
Output: 0
Explanation: The string is already alternating โ
Input: s = "1110"
Output: 1
Explanation:
"1010"1 <= s.length <= 10^5s[i] is either '0' or '1'Since rotation is allowed (Type-1), we must consider all cyclic shifts of the string.
For each possible rotation, compute the minimum flips required to match:
010101...101010...Return the minimum among all possibilities.
public class Main {
public static void main(String[] args) {
// ๐น Hardcoded Input
String s = "111000";
int result = minFlips(s);
System.out.println("Input: " + s);
System.out.println("Minimum Flips Required: " + result);
}
public static int minFlips(String s) {
int n = s.length();
// ๐น Double the string to simulate rotations
String doubled = s + s;
// ๐น Create alternating patterns of size 2n
StringBuilder alt1 = new StringBuilder(); // 010101...
StringBuilder alt2 = new StringBuilder(); // 101010...
for (int i = 0; i < 2 * n; i++) {
if (i % 2 == 0) {
alt1.append('0');
alt2.append('1');
} else {
alt1.append('1');
alt2.append('0');
}
}
int left = 0;
int diff1 = 0;
int diff2 = 0;
int answer = Integer.MAX_VALUE;
// ๐น Sliding window
for (int right = 0; right < 2 * n; right++) {
if (doubled.charAt(right) != alt1.charAt(right))
diff1++;
if (doubled.charAt(right) != alt2.charAt(right))
diff2++;
// Maintain window size = n
if (right - left + 1 > n) {
if (doubled.charAt(left) != alt1.charAt(left))
diff1--;
if (doubled.charAt(left) != alt2.charAt(left))
diff2--;
left++;
}
if (right - left + 1 == n) {
answer = Math.min(answer, Math.min(diff1, diff2));
}
}
return answer;
}
}
โจ Perfect problem to practice Sliding Window + Alternating Pattern Matching
โ Written by Saurabh Patil โข B.Tech CSE โข Software Developer๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.