8000 New Problem Solution - "1880. Check if Word Equals Summation of Two W… · royeLin/leetcode_cpp@d8e8f74 · GitHub
[go: up one dir, main page]

Skip to content

Commit d8e8f74

Browse files
committed
New Problem Solution - "1880. Check if Word Equals Summation of Two Words"
1 parent c19a4bb commit d8e8f74

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1880|[Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/) | [C++](./algorithms/cpp/checkIfWordEqualsSummationOfTwoWords/CheckIfWordEqualsSummationOfTwoWords.cpp)|Easy|
1213
|1871|[Jump Game VII](https://leetcode.com/problems/jump-game-vii/) | [C++](./algorithms/cpp/jumpGame/jumpGame.VII.cpp)|Medium|
1314
|1870|[Minimum Speed to Arrive on Time](https://leetcode.com/problems/minimum-speed-to-arrive-on-time/) | [C++](./algorithms/cpp/minimumSpeedToArriveOnTime/MinimumSpeedToArriveOnTime.cpp)|Medium|
1415
|1869|[Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/) | [C++](./algorithms/cpp/longerContiguousSegmentsOfOnesThanZeros/LongerContiguousSegmentsOfOnesThanZeros.cpp)|Easy|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)
0