|
| 1 | +// Source : https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-05-30 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> |
| 8 | + * 1, 'c' -> 2, etc.). |
| 9 | + * |
| 10 | + * The numerical value of some string of lowercase English letters s is the concatenation of the |
| 11 | + * letter values of each letter in s, which is then converted into an integer. |
| 12 | + * |
| 13 | + * For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". |
| 14 | + * After converting it, we get 21. |
| 15 | + * |
| 16 | + * You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase |
| 17 | + * English letters 'a' through 'j' inclusive. |
| 18 | + * |
| 19 | + * Return true if the summation of the numerical values of firstWord and secondWord equals the |
| 20 | + * numerical value of targetWord, or false otherwise. |
| 21 | + * |
| 22 | + * Example 1: |
| 23 | + * |
| 24 | + * Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb" |
| 25 | + * Output: true |
| 26 | + * Explanation: |
| 27 | + * The numerical value of firstWord is "acb" -> "021" -> 21. |
| 28 | + * The numerical value of secondWord is "cba" -> "210" -> 210. |
| 29 | + * The numerical value of targetWord is "cdb" -> "231" -> 231. |
| 30 | + * We return true because 21 + 210 == 231. |
| 31 | + * |
| 32 | + * Example 2: |
| 33 | + * |
| 34 | + * Input: firstWord = "aaa", secondWord = "a", targetWord = "aab" |
| 35 | + * Output: false |
| 36 | + * Explanation: |
| 37 | + * The numerical value of firstWord is "aaa" -> "000" -> 0. |
| 38 | + * The numerical value of secondWord is "a" -> "0" -> 0. |
| 39 | + * The numerical value of targetWord is "aab" -> "001" -> 1. |
| 40 | + * We return false because 0 + 0 != 1. |
| 41 | + * |
| 42 | + * Example 3: |
| 43 | + * |
| 44 | + * Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa" |
| 45 | + * Output: true |
| 46 | + * Explanation: |
| 47 | + * The numerical value of firstWord is "aaa" -> "000" -> 0. |
| 48 | + * The numerical value of secondWord is "a" -> "0" -> 0. |
| 49 | + * The numerical value of targetWord is "aaaa" -> "0000" -> 0. |
| 50 | + * We return true because 0 + 0 == 0. |
| 51 | + * |
| 52 | + * Constraints: |
| 53 | + * |
| 54 | + * 1 <= firstWord.length, secondWord.length, targetWord.length <= 8 |
| 55 | + * firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' |
| 56 | + * inclusive. |
| 57 | + ******************************************************************************************************/ |
| 58 | + |
| 59 | +class Solution { |
| 60 | +public: |
| 61 | + int strToInt(string& str) { |
| 62 | + int n = 0; |
| 63 | + for(auto& c :str) { |
| 64 | + n = n*10 + c - 'a'; |
| 65 | + } |
| 66 | + return n; |
| 67 | + } |
| 68 | + bool isSumEqual(string firstWord, string secondWord, string targetWord) { |
| 69 | + return strToInt(firstWord) + strToInt(secondWord) == strToInt(targetWord); |
| 70 | + } |
| 71 | +}; |
0 commit comments