Class Solution
-
- All Implemented Interfaces:
public final class Solution859 - Buddy Strings\.
Easy
Given two strings
sandgoal, returntrueif you can swap two letters insso the result is equal togoal, otherwise, returnfalse.Swapping letters is defined as taking two indices
iandj(0-indexed) such thati != jand swapping the characters ats[i]ands[j].For example, swapping at indices
0and2in"abcd"results in"cbad".
Example 1:
Input: s = "ab", goal = "ba"
Output: true
Explanation: You can swap s0 = 'a' and s1 = '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 s0 = 'a' and s1 = 'b', which results in "ba" != goal.
Example 3:
Input: s = "aa", goal = "aa"
Output: true
Explanation: You can swap s0 = 'a' and s1 = 'a' to get "aa", which is equal to goal.
Constraints:
<code>1 <= s.length, goal.length <= 2 * 10<sup>4</sup></code>
sandgoalconsist of lowercase letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleanbuddyStrings(String s, String goal)-
-
Method Detail
-
buddyStrings
final Boolean buddyStrings(String s, String goal)
-
-
-
-