LeetCode 题解工作台

检查某单词是否等于两单词之和

字母的 字母值 取决于字母在字母表中的位置, 从 0 开始 计数。即, 'a' -> 0 、 'b' -> 1 、 'c' -> 2 ,以此类推。 对某个由小写字母组成的字符串 s 而言,其 数值 就等于将 s 中每个字母的 字母值 按顺序 连接 并 转换 成对应整数。 例如, s = "acb" …

category

1

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

简单 · String-driven solution strategy

bolt

答案摘要

我们定义一个函数 ,用来计算字符串 的数值。对于字符串 中的每个字符 ,我们将其转换为对应的数字 ,然后将 依次连接起来,最后转换为整数。 最后,我们只需要判断 $\textit{f}(\textit{firstWord}) + \textit{f}(\textit{secondWord})$ 是否等于 即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

字母的 字母值 取决于字母在字母表中的位置,从 0 开始 计数。即,'a' -> 0'b' -> 1'c' -> 2,以此类推。

对某个由小写字母组成的字符串 s 而言,其 数值 就等于将 s 中每个字母的 字母值 按顺序 连接转换 成对应整数。

  • 例如,s = "acb" ,依次连接每个字母的字母值可以得到 "021" ,转换为整数得到 21

给你三个字符串 firstWordsecondWordtargetWord ,每个字符串都由从 'a''j'含 'a''j' )的小写英文字母组成。

如果 firstWord secondWord数值之和 等于 targetWord 的数值,返回 true ;否则,返回 false

 

示例 1:

输入:firstWord = "acb", secondWord = "cba", targetWord = "cdb"
输出:true
解释:
firstWord 的数值为 "acb" -> "021" -> 21
secondWord 的数值为 "cba" -> "210" -> 210
targetWord 的数值为 "cdb" -> "231" -> 231
由于 21 + 210 == 231 ,返回 true

示例 2:

输入:firstWord = "aaa", secondWord = "a", targetWord = "aab"
输出:false
解释:
firstWord 的数值为 "aaa" -> "000" -> 0
secondWord 的数值为 "a" -> "0" -> 0
targetWord 的数值为 "aab" -> "001" -> 1
由于 0 + 0 != 1 ,返回 false

示例 3:

输入:firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
输出:true
解释:
firstWord 的数值为 "aaa" -> "000" -> 0
secondWord 的数值为 "a" -> "0" -> 0
targetWord 的数值为 "aaaa" -> "0000" -> 0
由于 0 + 0 == 0 ,返回 true

 

提示:

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
  • firstWordsecondWordtargetWord 仅由从 'a''j'含 'a''j' )的小写英文字母组成
lightbulb

解题思路

方法一:字符串转数字

我们定义一个函数 f(s)\textit{f}(s),用来计算字符串 ss 的数值。对于字符串 ss 中的每个字符 cc,我们将其转换为对应的数字 xx,然后将 xx 依次连接起来,最后转换为整数。

最后,我们只需要判断 f(firstWord)+f(secondWord)\textit{f}(\textit{firstWord}) + \textit{f}(\textit{secondWord}) 是否等于 f(targetWord)\textit{f}(\textit{targetWord}) 即可。

时间复杂度 O(L)O(L),其中 LL 为题目中所有字符串的长度之和。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        def f(s: str) -> int:
            ans, a = 0, ord("a")
            for c in map(ord, s):
                x = c - a
                ans = ans * 10 + x
            return ans

        return f(firstWord) + f(secondWord) == f(targetWord)
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Evaluate if the candidate correctly implements the conversion of characters to numbers and ensures that the sum of the two words is compared to the target.

  • question_mark

    Look for understanding in string manipulation and handling of basic arithmetic operations.

  • question_mark

    Assess if the candidate is aware of edge cases, such as varying word lengths and the correct handling of smaller words.

warning

常见陷阱

外企场景
  • error

    Incorrect character-to-number conversion, such as mapping characters beyond 'a' to 'j'.

  • error

    Failing to account for the word length constraints, which could lead to incorrect calculations.

  • error

    Mistakes in arithmetic when summing the values or comparing the results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem by allowing uppercase letters (A-Z) or extending the word length constraint beyond 8.

  • arrow_right_alt

    Include additional operations, such as subtracting or multiplying the values of the words.

  • arrow_right_alt

    Challenge with varying the character set to include characters beyond 'a' to 'j'.

help

常见问题

外企场景

检查某单词是否等于两单词之和题解:String-driven solution … | LeetCode #1880 简单