Skip to content

Commit cc5d321

Browse files
committed
feat: update solutions to lc problem: No.1880. Check if Word Equals Summation of Two Words
1 parent 079e700 commit cc5d321

File tree

5 files changed

+98
-103
lines changed

5 files changed

+98
-103
lines changed

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,13 @@ targetWord 的数值为 "aaaa" -> "0000" -> 0
7474
```python
7575
class Solution:
7676
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
77-
n = 9
78-
firstWord = 'a' * (n - len(firstWord)) + firstWord
79-
secondWord = 'a' * (n - len(secondWord)) + secondWord
80-
targetWord = 'a' * (n - len(targetWord)) + targetWord
81-
carry = 0
82-
while n > 0:
83-
n -= 1
84-
i = ord(firstWord[n]) - ord('a')
85-
j = ord(secondWord[n]) - ord('a')
86-
carry, s = divmod(i + j + carry, 10)
87-
if targetWord[n] != chr(ord('a') + s):
88-
return False
89-
return True
77+
def transfer(word):
78+
res = 0
79+
for c in word:
80+
res *= 10
81+
res += (ord(c) - ord('a'))
82+
return res
83+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord)
9084
```
9185

9286
### **Java**
@@ -96,34 +90,40 @@ class Solution:
9690
```java
9791
class Solution {
9892
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
99-
int n = 9;
100-
firstWord = fillA(firstWord, n);
101-
secondWord = fillA(secondWord, n);
102-
targetWord = fillA(targetWord, n);
103-
int carry = 0;
104-
while (--n >= 0) {
105-
int i = firstWord.charAt(n) - 'a';
106-
int j = secondWord.charAt(n) - 'a';
107-
int s = i + j + carry;
108-
if (targetWord.charAt(n) != (char) ('a' + (s % 10))) {
109-
return false;
110-
}
111-
carry = s / 10;
112-
}
113-
return true;
93+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
11494
}
11595

116-
private String fillA(String word, int n) {
117-
StringBuilder sb = new StringBuilder();
118-
for (int i = 0; i < n - word.length(); ++i) {
119-
sb.append("a");
96+
private int transfer(String word) {
97+
int res = 0;
98+
for (char c : word.toCharArray()) {
99+
res *= 10;
100+
res += (c - 'a');
120101
}
121-
sb.append(word);
122-
return sb.toString();
102+
return res;
123103
}
124104
}
125105
```
126106

107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
113+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
114+
}
115+
private:
116+
int transfer(string word) {
117+
int res = 0;
118+
for (char c : word) {
119+
res *= 10;
120+
res += (c - 'a');
121+
}
122+
return res;
123+
}
124+
};
125+
```
126+
127127
### **...**
128128
129129
```

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -96,54 +96,54 @@ We return true because 0 + 0 == 0.
9696
```python
9797
class Solution:
9898
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
99-
n = 9
100-
firstWord = 'a' * (n - len(firstWord)) + firstWord
101-
secondWord = 'a' * (n - len(secondWord)) + secondWord
102-
targetWord = 'a' * (n - len(targetWord)) + targetWord
103-
carry = 0
104-
while n > 0:
105-
n -= 1
106-
i = ord(firstWord[n]) - ord('a')
107-
j = ord(secondWord[n]) - ord('a')
108-
carry, s = divmod(i + j + carry, 10)
109-
if targetWord[n] != chr(ord('a') + s):
110-
return False
111-
return True
99+
def transfer(word):
100+
res = 0
101+
for c in word:
102+
res *= 10
103+
res += (ord(c) - ord('a'))
104+
return res
105+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord)
112106
```
113107

114108
### **Java**
115109

116110
```java
117111
class Solution {
118112
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
119-
int n = 9;
120-
firstWord = fillA(firstWord, n);
121-
secondWord = fillA(secondWord, n);
122-
targetWord = fillA(targetWord, n);
123-
int carry = 0;
124-
while (--n >= 0) {
125-
int i = firstWord.charAt(n) - 'a';
126-
int j = secondWord.charAt(n) - 'a';
127-
int s = i + j + carry;
128-
if (targetWord.charAt(n) != (char) ('a' + (s % 10))) {
129-
return false;
130-
}
131-
carry = s / 10;
132-
}
133-
return true;
113+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
134114
}
135115

136-
private String fillA(String word, int n) {
137-
StringBuilder sb = new StringBuilder();
138-
for (int i = 0; i < n - word.length(); ++i) {
139-
sb.append("a");
116+
private int transfer(String word) {
117+
int res = 0;
118+
for (char c : word.toCharArray()) {
119+
res *= 10;
120+
res += (c - 'a');
140121
}
141-
sb.append(word);
142-
return sb.toString();
122+
return res;
143123
}
144124
}
145125
```
146126

127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
133+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
134+
}
135+
private:
136+
int transfer(string word) {
137+
int res = 0;
138+
for (char c : word) {
139+
res *= 10;
140+
res += (c - 'a');
141+
}
142+
return res;
143+
}
144+
};
145+
```
146+
147147
### **...**
148148
149149
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
4+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
5+
}
6+
private:
7+
int transfer(string word) {
8+
int res = 0;
9+
for (char c : word) {
10+
res *= 10;
11+
res += (c - 'a');
12+
}
13+
return res;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
11
class Solution {
22
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
3-
int n = 9;
4-
firstWord = fillA(firstWord, n);
5-
secondWord = fillA(secondWord, n);
6-
targetWord = fillA(targetWord, n);
7-
int carry = 0;
8-
while (--n >= 0) {
9-
int i = firstWord.charAt(n) - 'a';
10-
int j = secondWord.charAt(n) - 'a';
11-
int s = i + j + carry;
12-
if (targetWord.charAt(n) != (char) ('a' + (s % 10))) {
13-
return false;
14-
}
15-
carry = s / 10;
16-
}
17-
return true;
3+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
184
}
19-
20-
private String fillA(String word, int n) {
21-
StringBuilder sb = new StringBuilder();
22-
for (int i = 0; i < n - word.length(); ++i) {
23-
sb.append("a");
5+
6+
private int transfer(String word) {
7+
int res = 0;
8+
for (char c : word.toCharArray()) {
9+
res *= 10;
10+
res += (c - 'a');
2411
}
25-
sb.append(word);
26-
return sb.toString();
12+
return res;
2713
}
2814
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
class Solution:
22
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
3-
n = 9
4-
firstWord = 'a' * (n - len(firstWord)) + firstWord
5-
secondWord = 'a' * (n - len(secondWord)) + secondWord
6-
targetWord = 'a' * (n - len(targetWord)) + targetWord
7-
carry = 0
8-
while n > 0:
9-
n -= 1
10-
i = ord(firstWord[n]) - ord('a')
11-
j = ord(secondWord[n]) - ord('a')
12-
carry, s = divmod(i + j + carry, 10)
13-
if targetWord[n] != chr(ord('a') + s):
14-
return False
15-
return True
3+
def transfer(word):
4+
res = 0
5+
for c in word:
6+
res *= 10
7+
res += (ord(c) - ord('a'))
8+
return res
9+
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord)

0 commit comments

Comments
 (0)