Skip to content

Practice Buddy Strings

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#859
LeetCode Problem

Buddy Strings

Problem Statement

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].

Source: Buddy Strings on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "ab", goal = "ba"
Output
true

# Example 2

Input
s = "ab", goal = "ab"
Output
false

# Example 3

Input
s = "aa", goal = "aa"
Output
true

Constraints

1 <= s.length, goal.length <= 2 * 10^4
s and goal consist of lowercase letters.

Related LeetCode Problems

Practice more problems similar to Buddy Strings.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.