|
| 1 | +/** |
| 2 | + * 1880 |
| 3 | + * Check if Word Equals Summation of Two Words |
| 4 | + ** |
| 5 | + * The letter value of a letter is |
| 6 | + * its position in the alphabet starting from 0 |
| 7 | + * (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.). |
| 8 | + * |
| 9 | + * The numerical value of some string of lowercase English letters s |
| 10 | + * is the concatenation of the letter values of each letter in s, |
| 11 | + * which is then converted into an integer. |
| 12 | + * |
| 13 | + * For example, |
| 14 | + * if s = "acb", |
| 15 | + * we concatenate each letter's letter value, |
| 16 | + * resulting in "021". |
| 17 | + * After converting it, we get 21. |
| 18 | + * You are given three strings |
| 19 | + * firstWord, secondWord, and targetWord, |
| 20 | + * each consisting of lowercase English letters 'a' through 'j' inclusive. |
| 21 | + * |
| 22 | + * Return true |
| 23 | + * if the summation of the numerical values of firstWord and secondWord equals |
| 24 | + * the numerical value of targetWord, |
| 25 | + * or false otherwise. |
| 26 | + * |
| 27 | + * Example 1: |
| 28 | + * Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb" |
| 29 | + * Output: true |
| 30 | + * Explanation: |
| 31 | + * The numerical value of firstWord is "acb" -> "021" -> 21. |
| 32 | + * The numerical value of secondWord is "cba" -> "210" -> 210. |
| 33 | + * The numerical value of targetWord is "cdb" -> "231" -> 231. |
| 34 | + * We return true because 21 + 210 == 231. |
| 35 | + * |
| 36 | + * Example 2: |
| 37 | + * Input: firstWord = "aaa", secondWord = "a", targetWord = "aab" |
| 38 | + * Output: false |
| 39 | + * Explanation: |
| 40 | + * The numerical value of firstWord is "aaa" -> "000" -> 0. |
| 41 | + * The numerical value of secondWord is "a" -> "0" -> 0. |
| 42 | + * The numerical value of targetWord is "aab" -> "001" -> 1. |
| 43 | + * We return false because 0 + 0 != 1. |
| 44 | + * |
| 45 | + * Example 3: |
| 46 | + * Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa" |
| 47 | + * Output: true |
| 48 | + * Explanation: |
| 49 | + * The numerical value of firstWord is "aaa" -> "000" -> 0. |
| 50 | + * The numerical value of secondWord is "a" -> "0" -> 0. |
| 51 | + * The numerical value of targetWord is "aaaa" -> "0000" -> 0. |
| 52 | + * We return true because 0 + 0 == 0. |
| 53 | + * |
| 54 | + * Constraints: |
| 55 | + * • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8 |
| 56 | + * • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive. |
| 57 | + * |
| 58 | + * Hint 1: |
| 59 | + * Convert each character of each word to its numerical value. |
| 60 | + * |
| 61 | + * Hint 2: |
| 62 | + * Check if the numerical values satisfies the condition. |
| 63 | + ** |
| 64 | + * https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/ |
| 65 | +***/ |
| 66 | + |
| 67 | +namespace Problems; |
| 68 | + |
| 69 | +public class CheckIfWordEqualsSummationOfTwoWords |
| 70 | +{ |
| 71 | + public bool IsSumEqual( string firstWord, string secondWord, string targetWord ) |
| 72 | + { |
| 73 | + return LetterValue( firstWord ) + LetterValue( secondWord ) == LetterValue( targetWord ); |
| 74 | + } |
| 75 | + |
| 76 | + private int LetterValue( string word ) |
| 77 | + { |
| 78 | + int result = 0; |
| 79 | + |
| 80 | + foreach ( char c in word ) |
| 81 | + { |
| 82 | + result = result * 10 + ( c - 'a' ); |
| 83 | + } |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | +} |
0 commit comments