859. Buddy Strings

Description

Given two strings A and B of lowercase letters, return true if you can swap two letters in A so the result is equal to B, 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 A[i] and A[j]. For example, swapping at indices 0 and 2 in "abcd" results in "cbad".

Constraints

  • 0 <= A.length <= 20000

  • 0 <= B.length <= 20000

  • A and B consist of lowercase letters.

Approach

  • GeeksforGeeks

  • ProgramCreek

  • YouTube

Examples

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.

Solutions

Follow up

Last updated

Was this helpful?