返回介绍

solution / 1800-1899 / 1880.Check if Word Equals Summation of Two Words / README_EN

发布于 2024-06-17 01:03:13 字数 6549 浏览 0 评论 0 收藏 0

1880. Check if Word Equals Summation of Two Words

中文文档

Description

The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.).

The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.

  • For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21.

You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive.

Return true _if the summation of the numerical values of _firstWord_ and _secondWord_ equals the numerical value of _targetWord_, or _false_ otherwise._

 

Example 1:

Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb"
Output: true
Explanation:
The numerical value of firstWord is "acb" -> "021" -> 21.
The numerical value of secondWord is "cba" -> "210" -> 210.
The numerical value of targetWord is "cdb" -> "231" -> 231.
We return true because 21 + 210 == 231.

Example 2:

Input: firstWord = "aaa", secondWord = "a", targetWord = "aab"
Output: false
Explanation: 
The numerical value of firstWord is "aaa" -> "000" -> 0.
The numerical value of secondWord is "a" -> "0" -> 0.
The numerical value of targetWord is "aab" -> "001" -> 1.
We return false because 0 + 0 != 1.

Example 3:

Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
Output: true
Explanation: 
The numerical value of firstWord is "aaa" -> "000" -> 0.
The numerical value of secondWord is "a" -> "0" -> 0.
The numerical value of targetWord is "aaaa" -> "0000" -> 0.
We return true because 0 + 0 == 0.

 

Constraints:

  • 1 <= firstWord.length,secondWord.length,targetWord.length <= 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.

Solutions

Solution 1

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)
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;
  }
}
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);
}
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)
  }
}
/**
 * @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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文