LeetCode 题解工作台

判断操作后字符串中的数字是否相等 I

给你一个由数字组成的字符串 s 。重复执行以下操作,直到字符串恰好包含 两个 数字: 从第一个数字开始,对于 s 中的每一对连续数字,计算这两个数字的和 模 10。 用计算得到的新数字依次替换 s 的每一个字符,并保持原本的顺序。 如果 s 最后剩下的两个数字 相同 ,返回 true 。否则,返回 …

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·string

bolt

答案摘要

我们可以模拟题目描述的操作,直到字符串 中只剩下两个数字为止,判断这两个数字是否相同。 时间复杂度 ,空间复杂度 。其中 为字符串 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个由数字组成的字符串 s 。重复执行以下操作,直到字符串恰好包含 两个 数字:

  • 从第一个数字开始,对于 s 中的每一对连续数字,计算这两个数字的和 模 10。
  • 用计算得到的新数字依次替换 s 的每一个字符,并保持原本的顺序。

如果 s 最后剩下的两个数字 相同 ,返回 true 。否则,返回 false

 

示例 1:

输入: s = "3902"

输出: true

解释:

  • 一开始,s = "3902"
  • 第一次操作:
    • (s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
    • (s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
    • (s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
    • s 变为 "292"
  • 第二次操作:
    • (s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
    • (s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
    • s 变为 "11"
  • 由于 "11" 中的数字相同,输出为 true

示例 2:

输入: s = "34789"

输出: false

解释:

  • 一开始,s = "34789"
  • 第一次操作后,s = "7157"
  • 第二次操作后,s = "862"
  • 第三次操作后,s = "48"
  • 由于 '4' != '8',输出为 false

 

提示:

  • 3 <= s.length <= 100
  • s 仅由数字组成。
lightbulb

解题思路

方法一:模拟

我们可以模拟题目描述的操作,直到字符串 ss 中只剩下两个数字为止,判断这两个数字是否相同。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n)O(n)。其中 nn 为字符串 ss 的长度。

1
2
3
4
5
6
7
8
9
class Solution:
    def hasSameDigits(self, s: str) -> bool:
        t = list(map(int, s))
        n = len(t)
        for k in range(n - 1, 1, -1):
            for i in range(k):
                t[i] = (t[i] + t[i + 1]) % 10
        return t[0] == t[1]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The prompt says to perform the operation repeatedly as described, which strongly signals direct simulation instead of a hidden data structure trick.

  • question_mark

    Math plus String is the right pattern because each new digit comes from local arithmetic on adjacent characters, not from parsing the whole number.

  • question_mark

    An interviewer may watch whether you preserve the exact modulo 10 reduction on every pair, since that is the easiest place to misread the rule.

warning

常见陷阱

外企场景
  • error

    Using normal addition without modulo 10, which breaks cases where adjacent digits sum to 10 or more.

  • error

    Stopping after one pass or comparing the first two digits seen, instead of continuing until the string length is exactly 2.

  • error

    Building the next row in place from left to right without care, which can overwrite digits that are still needed for later adjacent sums in the same round.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return the final two-digit string instead of a boolean, which keeps the same repeated adjacent-sum simulation.

  • arrow_right_alt

    Change the operation to multiply adjacent digits or use a different modulus, which keeps the shrinking process but changes the arithmetic rule.

  • arrow_right_alt

    Ask for an optimized math derivation of the final digits using combinatorics, which is more relevant in a follow-up than in this Easy version.

help

常见问题

外企场景

判断操作后字符串中的数字是否相等 I题解:数学·string | LeetCode #3461 简单