Package g0801_0900.s0859_buddy_strings
Class Solution
- java.lang.Object
-
- g0801_0900.s0859_buddy_strings.Solution
-
public class Solution extends Object
859 - Buddy Strings.Easy
Given two strings
s
andgoal
, returntrue
if you can swap two letters ins
so the result is equal togoal
, otherwise, returnfalse
.Swapping letters is defined as taking two indices
i
andj
(0-indexed) such thati != j
and swapping the characters ats[i]
ands[j]
.- For example, swapping at indices
0
and2
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 * 104
s
andgoal
consist of lowercase letters.
- For example, swapping at indices
-
-
Constructor Summary
Constructors Constructor Description Solution()
-