Class Solution

java.lang.Object
g0801_0900.s0859_buddy_strings.Solution

public class Solution extends Object
859 - Buddy Strings.<p>Easy</p> <p>Given two strings <code>s</code> and <code>goal</code>, return <code>true</code> <em>if you can swap two letters in</em> <code>s</code> <em>so the result is equal to</em> <code>goal</code><em>, otherwise, return</em> <code>false</code><em>.</em></p> <p>Swapping letters is defined as taking two indices <code>i</code> and <code>j</code> (0-indexed) such that <code>i != j</code> and swapping the characters at <code>s[i]</code> and <code>s[j]</code>.</p> <ul> <li>For example, swapping at indices <code>0</code> and <code>2</code> in <code>&quot;abcd&quot;</code> results in <code>&quot;cbad&quot;</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;ab&rdquo;, goal = &ldquo;ba&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> You can swap s[0] = &lsquo;a&rsquo; and s[1] = &lsquo;b&rsquo; to get &ldquo;ba&rdquo;, which is equal to goal.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;ab&rdquo;, goal = &ldquo;ab&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The only letters you can swap are s[0] = &lsquo;a&rsquo; and s[1] = &lsquo;b&rsquo;, which results in &ldquo;ba&rdquo; != goal.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;aa&rdquo;, goal = &ldquo;aa&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> You can swap s[0] = &lsquo;a&rsquo; and s[1] = &lsquo;a&rsquo; to get &ldquo;aa&rdquo;, which is equal to goal.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length, goal.length <= 2 * 10<sup>4</sup></code></li> <li><code>s</code> and <code>goal</code> consist of lowercase letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • buddyStrings

      public boolean buddyStrings(String s, String goal)