Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Special binary strings are binary strings with the following two properties:
0's is equal to the number of 1's.1's as 0's.You are given a special binary string s.
A move consists of choosing two consecutive, non-empty, special substrings of s, and swapping them. Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.
Return the lexicographically largest resulting string possible after applying the mentioned operations on the string.
Example 1:
Input: s = "11011000" Output: "11100100" Explanation: The strings "10" [occuring at s[1]] and "1100" [at s[3]] are swapped. This is the lexicographically largest string possible after some number of swaps.
Example 2:
Input: s = "10" Output: "10"
Constraints:
1 <= s.length <= 50s[i] is either '0' or '1'.s is a special binary string.Problem summary: Special binary strings are binary strings with the following two properties: The number of 0's is equal to the number of 1's. Every prefix of the binary string has at least as many 1's as 0's. You are given a special binary string s. A move consists of choosing two consecutive, non-empty, special substrings of s, and swapping them. Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string. Return the lexicographically largest resulting string possible after applying the mentioned operations on the string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"11011000"
"10"
valid-parenthesis-string)number-of-good-binary-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #761: Special Binary String
class Solution {
public String makeLargestSpecial(String s) {
if ("".equals(s)) {
return "";
}
List<String> ans = new ArrayList<>();
int cnt = 0;
for (int i = 0, j = 0; i < s.length(); ++i) {
cnt += s.charAt(i) == '1' ? 1 : -1;
if (cnt == 0) {
String t = "1" + makeLargestSpecial(s.substring(j + 1, i)) + "0";
ans.add(t);
j = i + 1;
}
}
ans.sort(Comparator.reverseOrder());
return String.join("", ans);
}
}
// Accepted solution for LeetCode #761: Special Binary String
func makeLargestSpecial(s string) string {
if s == "" {
return ""
}
ans := sort.StringSlice{}
cnt := 0
for i, j := 0, 0; i < len(s); i++ {
if s[i] == '1' {
cnt++
} else {
cnt--
}
if cnt == 0 {
ans = append(ans, "1"+makeLargestSpecial(s[j+1:i])+"0")
j = i + 1
}
}
sort.Sort(sort.Reverse(ans))
return strings.Join(ans, "")
}
# Accepted solution for LeetCode #761: Special Binary String
class Solution:
def makeLargestSpecial(self, s: str) -> str:
if s == '':
return ''
ans = []
cnt = 0
i = j = 0
while i < len(s):
cnt += 1 if s[i] == '1' else -1
if cnt == 0:
ans.append('1' + self.makeLargestSpecial(s[j + 1 : i]) + '0')
j = i + 1
i += 1
ans.sort(reverse=True)
return ''.join(ans)
// Accepted solution for LeetCode #761: Special Binary String
struct Solution;
use std::cmp::Reverse;
impl Solution {
fn make_largest_special(s: String) -> String {
let mut count = 0;
let mut v = vec![];
let mut ss = "".to_string();
for c in s.chars() {
ss.push(c);
if c == '1' {
count += 1;
} else {
count -= 1;
}
if count == 0 {
let n = ss.len();
let t = format!("1{}0", Self::make_largest_special(ss[1..n - 1].to_string()));
v.push(t);
ss = "".to_string();
}
}
v.sort_unstable_by_key(|s| Reverse(s.to_string()));
v.join("")
}
}
#[test]
fn test() {
let s = "11011000".to_string();
let res = "11100100".to_string();
assert_eq!(Solution::make_largest_special(s), res);
}
// Accepted solution for LeetCode #761: Special Binary String
function makeLargestSpecial(s: string): string {
if (s.length === 0) {
return '';
}
const ans: string[] = [];
let cnt = 0;
for (let i = 0, j = 0; i < s.length; ++i) {
cnt += s[i] === '1' ? 1 : -1;
if (cnt === 0) {
const t = '1' + makeLargestSpecial(s.substring(j + 1, i)) + '0';
ans.push(t);
j = i + 1;
}
}
ans.sort((a, b) => b.localeCompare(a));
return ans.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.