Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    859 - Buddy Strings\.

    Easy

    Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, 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 s[i] and s[j].

    • For example, swapping at indices 0 and 2 in "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>

    • s and goal consist of lowercase letters.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Boolean buddyStrings(String s, String goal)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait