859. Buddy Strings
Given two strings A and B of lowercase letters, return true if you can swap two letters inA so the result is equal toB , otherwise, returnfalse .
Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at A[i] and A[j]. For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
Example 1:
Input: A = "ab", B = "ba"
Output: true
**Explanation** **:** You can swap A[0] = 'a' and A[1] = 'b' to get "ba", which is equal to B.Example 2:
Input: A = "ab", B = "ab"
Output: false
**Explanation** **:** The only letters you can swap are A[0] = 'a' and A[1] = 'b', which results in "ba" != B.Example 3:
Input: A = "aa", B = "aa"
Output: true
**Explanation** **:** You can swap A[0] = 'a' and A[1] = 'a' to get "aa", which is equal to B.Example 4:
Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: trueExample 5:
Input: A = "", B = "aa"
Output: falseConstraints:
0 <= A.length <= 200000 <= B.length <= 20000AandBconsist of lowercase letters.
# @lc code=start
using LeetCode
function buddy_strings(A::String, B::String)::Bool
if length(A) == length(B)
different_pairs = Pair{Char,Char}[]
for (a, b) in zip(A, B)
if a != b
push!(different_pairs, a => b)
end
end
if length(different_pairs) == 0
# check if A contains at least 2 duplicate letters
length(Set(A)) < length(A)
elseif length(different_pairs) == 2
((x, y), (z, w)) = different_pairs
x == w && y == z
else
false
end
else
false
end
end
# @lc code=endbuddy_strings (generic function with 1 method)
This page was generated using DemoCards.jl and Literate.jl.