↓Skip to main content
  1. LeetCode/

LeetCode 859: Buddy Strings

·2 mins· ·
LeetCode Easy Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-17

  • Submissions: Runtime Error ×2 → Wrong Answer ×3 → Accepted ×3

Approach
#

Three cases:

  1. Different lengths: impossible.
  2. s == goal: the swap must exchange two equal characters, so s needs a repeat (len(set(s)) < len(s)).
  3. Otherwise collect the differing positions; there must be exactly two, i, j, with s[i] == goal[j] and s[j] == goal[i].

My Attempts
#

This took 8 submissions, each adding one case:

  • The first two had variable bugs: the counter was incremented before d1 and d2 were set, so d1 was never assigned (NameError).
  • Attempt 3 ignored identical strings: "c", "c" must still swap once, impossible without a repeated character, so the answer is False; it returned True.
  • After that fix, a single differing position still returned True: "a", "c". Failing on exactly one difference passed.

Solution
#

class Solution:
    def buddyStrings(self, s: str, goal: str) -> bool:
        if len(s) != len(goal):
            return False
        if s == goal:
            return len(set(s)) < len(s)
        diff = [i for i in range(len(s)) if s[i] != goal[i]]
        return len(diff) == 2 and s[diff[0]] == goal[diff[1]] and s[diff[1]] == goal[diff[0]]

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • One swap is mandatory: identical strings qualify only with a repeated character.
  • With a single differing position no swap can fix it.

Interview Explanation
#

I rule out different lengths; identical strings need a repeated character to swap; otherwise there must be exactly two differing positions that cross-match. Time O(n).

Related

LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 771: Jewels and Stones
·1 min
LeetCode Easy Hash-Table String
LeetCode 771 note: put jewels in a set and count how many stones are in it
LeetCode 748: Shortest Completing Word
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 748 note: count the plate’s letters in lowercase, then find the shortest word covering every count, taking the earliest on ties
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 290: Word Pattern
·1 min
LeetCode Easy Hash-Table String
LeetCode 290 note: pattern characters and words must correspond one to one; use two dicts or compare set sizes
LeetCode 383: Ransom Note
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 383 note: every character’s demand in ransomNote must not exceed its supply in magazine; an empty Counter difference means yes