1880. Check if Word Equals Summation of Two Words LeetCode Solution

In this guide, you will get 1880. Check if Word Equals Summation of Two Words LeetCode Solution with the best time and space complexity. The solution to Check if Word Equals Summation of Two Words problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Word Equals Summation of Two Words solution in C++
  4. Check if Word Equals Summation of Two Words solution in Java
  5. Check if Word Equals Summation of Two Words solution in Python
  6. Additional Resources
1880. Check if Word Equals Summation of Two Words LeetCode Solution image

Problem Statement of Check if Word Equals Summation of Two Words

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.

See also  2554. Maximum Number of Integers to Choose From a Range I LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1880. Check if Word Equals Summation of Two Words LeetCode Solution in C++

class Solution {
 public:
  bool isSumEqual(string firstWord, string secondWord, string targetWord) {
    const int first = getNumber(firstWord);
    const int second = getNumber(secondWord);
    const int target = getNumber(targetWord);
    return first + second == target;
  }

 private:
  int getNumber(const string& word) {
    int num = 0;
    for (const char c : word)
      num = num * 10 + (c - 'a');
    return num;
  }
};
/* code provided by PROGIEZ */

1880. Check if Word Equals Summation of Two Words LeetCode Solution in Java

class Solution {
  public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
    final int first = getNumber(firstWord);
    final int second = getNumber(secondWord);
    final int target = getNumber(targetWord);
    return first + second == target;
  }

  private int getNumber(String word) {
    int num = 0;
    for (final char c : word.toCharArray())
      num = num * 10 + (c - 'a');
    return num;
  }
}
// code provided by PROGIEZ

1880. Check if Word Equals Summation of Two Words LeetCode Solution in Python

class Solution:
  def isSumEqual(
      self,
      firstWord: str,
      secondWord: str,
      targetWord: str,
  ) -> bool:
    first = self._getNumber(firstWord)
    second = self._getNumber(secondWord)
    target = self._getNumber(targetWord)
    return first + second == target

  def _getNumber(self, word: str) -> int:
    num = 0
    for c in word:
      num = num * 10 + ord(c) - ord('a')
    return num
# code by PROGIEZ

Additional Resources

See also  3475. DNA Pattern Recognition LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.