Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.
Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
0 and 2 in "abcd" results in "cbad".Example 1:
Input: s = "ab", goal = "ba" Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
Example 2:
Input: s = "ab", goal = "ab" Output: false Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
Example 3:
Input: s = "aa", goal = "aa" Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
Constraints:
1 <= s.length, goal.length <= 2 * 104s and goal consist of lowercase letters.Problem summary: Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j]. For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"ab" "ba"
"ab" "ab"
"aa" "aa"
determine-if-two-strings-are-close)check-if-one-string-swap-can-make-strings-equal)make-number-of-distinct-characters-equal)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #859: Buddy Strings
class Solution {
public boolean buddyStrings(String s, String goal) {
int m = s.length(), n = goal.length();
if (m != n) {
return false;
}
int diff = 0;
int[] cnt1 = new int[26];
int[] cnt2 = new int[26];
for (int i = 0; i < n; ++i) {
int a = s.charAt(i), b = goal.charAt(i);
++cnt1[a - 'a'];
++cnt2[b - 'a'];
if (a != b) {
++diff;
}
}
boolean f = false;
for (int i = 0; i < 26; ++i) {
if (cnt1[i] != cnt2[i]) {
return false;
}
if (cnt1[i] > 1) {
f = true;
}
}
return diff == 2 || (diff == 0 && f);
}
}
// Accepted solution for LeetCode #859: Buddy Strings
func buddyStrings(s string, goal string) bool {
m, n := len(s), len(goal)
if m != n {
return false
}
diff := 0
cnt1 := make([]int, 26)
cnt2 := make([]int, 26)
for i := 0; i < n; i++ {
cnt1[s[i]-'a']++
cnt2[goal[i]-'a']++
if s[i] != goal[i] {
diff++
}
}
f := false
for i := 0; i < 26; i++ {
if cnt1[i] != cnt2[i] {
return false
}
if cnt1[i] > 1 {
f = true
}
}
return diff == 2 || (diff == 0 && f)
}
# Accepted solution for LeetCode #859: Buddy Strings
class Solution:
def buddyStrings(self, s: str, goal: str) -> bool:
m, n = len(s), len(goal)
if m != n:
return False
cnt1, cnt2 = Counter(s), Counter(goal)
if cnt1 != cnt2:
return False
diff = sum(s[i] != goal[i] for i in range(n))
return diff == 2 or (diff == 0 and any(v > 1 for v in cnt1.values()))
// Accepted solution for LeetCode #859: Buddy Strings
struct Solution;
use std::collections::HashSet;
impl Solution {
fn buddy_strings(a: String, b: String) -> bool {
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
let n = a.len();
let m = b.len();
if n != m {
return false;
}
if a == b {
let mut hs: HashSet<char> = HashSet::new();
let mut sum = 0;
for &c in &a {
if !hs.insert(c) {
sum += 1;
}
}
sum != 0
} else {
let mut pair: Vec<usize> = vec![];
for i in 0..n {
if a[i] != b[i] {
pair.push(i);
}
}
if pair.len() == 2 {
let i = pair[0];
let j = pair[1];
a[i] == b[j] && a[j] == b[i]
} else {
false
}
}
}
}
#[test]
fn test() {
let a = "ab".to_string();
let b = "ba".to_string();
assert_eq!(Solution::buddy_strings(a, b), true);
let a = "ab".to_string();
let b = "ab".to_string();
assert_eq!(Solution::buddy_strings(a, b), false);
let a = "aa".to_string();
let b = "aa".to_string();
assert_eq!(Solution::buddy_strings(a, b), true);
let a = "aaaaaaabc".to_string();
let b = "aaaaaaacb".to_string();
assert_eq!(Solution::buddy_strings(a, b), true);
let a = "".to_string();
let b = "aa".to_string();
assert_eq!(Solution::buddy_strings(a, b), false);
}
// Accepted solution for LeetCode #859: Buddy Strings
function buddyStrings(s: string, goal: string): boolean {
const m = s.length;
const n = goal.length;
if (m != n) {
return false;
}
const cnt1 = new Array(26).fill(0);
const cnt2 = new Array(26).fill(0);
let diff = 0;
for (let i = 0; i < n; ++i) {
cnt1[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
cnt2[goal.charCodeAt(i) - 'a'.charCodeAt(0)]++;
if (s[i] != goal[i]) {
++diff;
}
}
for (let i = 0; i < 26; ++i) {
if (cnt1[i] != cnt2[i]) {
return false;
}
}
return diff == 2 || (diff == 0 && cnt1.some(v => v > 1));
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.