LeetCode 题解工作台

亲密字符串

给你两个字符串 s 和 goal ,只要我们可以通过交换 s 中的两个字母得到与 goal 相等的结果,就返回 true ;否则返回 false 。 交换字母的定义是:取两个下标 i 和 j (下标从 0 开始)且满足 i != j ,接着交换 s[i] 和 s[j] 处的字符。 例如,在 "abc…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 哈希·表·结合·string

bolt

答案摘要

首先,先理解亲密字符串的意思: - 若两个字符串的长度或字符出现的频数不等,一定不是亲密字符串;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 哈希·表·结合·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个字符串 sgoal ,只要我们可以通过交换 s 中的两个字母得到与 goal 相等的结果,就返回 true ;否则返回 false

交换字母的定义是:取两个下标 ij (下标从 0 开始)且满足 i != j ,接着交换 s[i]s[j] 处的字符。

  • 例如,在 "abcd" 中交换下标 0 和下标 2 的元素可以生成 "cbad"

 

示例 1:

输入:s = "ab", goal = "ba"
输出:true
解释:你可以交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 相等。

示例 2:

输入:s = "ab", goal = "ab"
输出:false
解释:你只能交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 不相等。

示例 3:

输入:s = "aa", goal = "aa"
输出:true
解释:你可以交换 s[0] = 'a' 和 s[1] = 'a' 生成 "aa",此时 s 和 goal 相等。

 

提示:

  • 1 <= s.length, goal.length <= 2 * 104
  • sgoal 由小写英文字母组成
lightbulb

解题思路

方法一:字符统计

首先,先理解亲密字符串的意思:

  • 若两个字符串的长度或字符出现的频数不等,一定不是亲密字符串;
  • 若两个字符串对应位置不相等的字符数量为 2,或者数量为 0 并且字符串存在两个相同字符,则是亲密字符串。

因此,我们先判断两个字符串长度,若不等,直接返回 false

接着,统计两个字符串的字符频数,记为 cnt1cnt2,若 cnt1 不等于 cnt2,直接返回 false

然后枚举两个字符串,统计对应位置不相等的字符数量,若为 2,则返回 true;若为 0,且字符串存在两个相同字符,则返回 true

时间复杂度 O(n)O(n),空间复杂度 O(C)O(C)。其中 nn 是字符串 sgoal 的长度;而 CC 为字符集大小。

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def buddyStrings(self, s: str, goal: str) -> bool:
        m, n = len(s), len(goal)
        if m != n:
            return False
        cnt1, cnt2 = Counter(s), Counter(goal)
        if cnt1 != cnt2:
            return False
        diff = sum(s[i] != goal[i] for i in range(n))
        return diff == 2 or (diff == 0 and any(v > 1 for v in cnt1.values()))
speed

复杂度分析

指标
时间complexity is O(n) because each string is traversed once and character counts are tracked. Space complexity is O(1) or O(26) since only lowercase letters are counted using a fixed-size hash table.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Asks if swapping two letters is sufficient when s and goal are identical with duplicates.

  • question_mark

    Probes whether the candidate handles strings of different lengths correctly.

  • question_mark

    Wants reasoning behind using a hash table for character frequency versus simple mismatch tracking.

warning

常见陷阱

外企场景
  • error

    Failing to check for duplicate characters when s equals goal, missing valid swap cases.

  • error

    Assuming any two mismatches can be swapped without verifying order matches goal.

  • error

    Overcomplicating with nested loops instead of linear scan plus hash table.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow swapping any number of letters and check if s can become goal using minimal swaps.

  • arrow_right_alt

    Determine the number of distinct swaps needed to convert s to goal instead of just verifying one.

  • arrow_right_alt

    Extend to uppercase and mixed-case strings, adjusting hash table or frequency array accordingly.

help

常见问题

外企场景

亲密字符串题解:哈希·表·结合·string | LeetCode #859 简单