题目描述
字母的 字母值 取决于字母在字母表中的位置,从 0 开始 计数。即,'a' -> 0
、'b' -> 1
、'c' -> 2
,以此类推。
对某个由小写字母组成的字符串 s
而言,其 数值 就等于将 s
中每个字母的 字母值 按顺序 连接 并 转换 成对应整数。
- 例如,
s = "acb"
,依次连接每个字母的字母值可以得到 "021"
,转换为整数得到 21
。
给你三个字符串 firstWord
、secondWord
和 targetWord
,每个字符串都由从 '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
firstWord
、secondWord
和 targetWord
仅由从 'a'
到 'j'
(含 'a'
和 'j'
)的小写英文字母组成。
解法
方法一
| class Solution:
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
def f(s):
res = 0
for c in s:
res = res * 10 + (ord(c) - ord('a'))
return res
return f(firstWord) + f(secondWord) == f(targetWord)
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Solution {
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
return f(firstWord) + f(secondWord) == f(targetWord);
}
private int f(String s) {
int res = 0;
for (char c : s.toCharArray()) {
res = res * 10 + (c - 'a');
}
return res;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | class Solution {
public:
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
return f(firstWord) + f(secondWord) == f(targetWord);
}
int f(string s) {
int res = 0;
for (char c : s) res = res * 10 + (c - 'a');
return res;
}
};
|
| func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
f := func(s string) int {
res := 0
for _, c := range s {
res = res*10 + int(c-'a')
}
return res
}
return f(firstWord)+f(secondWord) == f(targetWord)
}
|
| function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean {
const calc = (s: string) => {
let res = 0;
for (const c of s) {
res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0);
}
return res;
};
return calc(firstWord) + calc(secondWord) === calc(targetWord);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | impl Solution {
fn calc(s: &String) -> i32 {
let mut res = 0;
for c in s.as_bytes() {
res = res * 10 + ((c - b'a') as i32);
}
res
}
pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word)
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | /**
* @param {string} firstWord
* @param {string} secondWord
* @param {string} targetWord
* @return {boolean}
*/
var isSumEqual = function (firstWord, secondWord, targetWord) {
function f(s) {
let res = 0;
for (let c of s) {
res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt());
}
return res;
}
return f(firstWord) + f(secondWord) == f(targetWord);
};
|
| int calc(char* s) {
int res = 0;
for (int i = 0; s[i]; i++) {
res = res * 10 + s[i] - 'a';
}
return res;
}
bool isSumEqual(char* firstWord, char* secondWord, char* targetWord) {
return calc(firstWord) + calc(secondWord) == calc(targetWord);
}
|