Formatted question description: https://leetcode.ca/all/1880.html

1880. Check if Word Equals Summation of Two Words

Level

Easy

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.

Solution

Convert firstWord, secondWord and targetWord into corresponding numerical values and check whether the sum of the former two numerical values equals the last numerical value.

  • class Solution {
        public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
            StringBuffer sb1 = new StringBuffer();
            StringBuffer sb2 = new StringBuffer();
            StringBuffer sb3 = new StringBuffer();
            int length1 = firstWord.length(), length2 = secondWord.length(), length3 = targetWord.length();
            for (int i = 0; i < length1; i++)
                sb1.append((char) (firstWord.charAt(i) - 'a' + '0'));
            for (int i = 0; i < length2; i++)
                sb2.append((char) (secondWord.charAt(i) - 'a' + '0'));
            for (int i = 0; i < length3; i++)
                sb3.append((char) (targetWord.charAt(i) - 'a' + '0'));
            int num1 = Integer.parseInt(sb1.toString());
            int num2 = Integer.parseInt(sb2.toString());
            int num3 = Integer.parseInt(sb3.toString());
            return num1 + num2 == num3;
        }
    }
    
  • // OJ: https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/
    // Time: O(N)
    // Space: O(1)
    class Solution {
        long num(string &w) {
            long ans = 0;
            for (char c : w) ans = ans * 10 + (c - 'a');
            return ans;
        }
    public:
        bool isSumEqual(string firstWord, string secondWord, string targetWord) {
            return num(firstWord) + num(secondWord) == num(targetWord);
        }
    };
    
  • # 1880. Check if Word Equals Summation of Two Words
    # https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/
    
    class Solution:
        def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
            
            def getNum(s):
                res = ""
                
                for char in s:
                    c = ord(char) - ord('a')
                    res += str(c)
                
                return int(res)
            
            return getNum(firstWord) + getNum(secondWord) == getNum(targetWord)
    
    

All Problems

All Solutions