Skip to content

Commit 0ed0a61

Browse files
committed
feat: add solutions to lc problem: No.1880
No.1880.Check if Word Equals Summation of Two Words
1 parent 7449cf4 commit 0ed0a61

File tree

5 files changed

+168
-22
lines changed

5 files changed

+168
-22
lines changed

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

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ public:
120120
};
121121
```
122122

123+
### **Go**
124+
125+
```go
126+
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
127+
f := func(s string) int {
128+
res := 0
129+
for _, c := range s {
130+
res = res*10 + int(c-'a')
131+
}
132+
return res
133+
}
134+
return f(firstWord)+f(secondWord) == f(targetWord)
135+
}
136+
```
137+
123138
### **JavaScript**
124139

125140
```js
@@ -141,18 +156,57 @@ var isSumEqual = function (firstWord, secondWord, targetWord) {
141156
};
142157
```
143158

144-
### **Go**
159+
### **TypeScript**
145160

146-
```go
147-
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
148-
f := func(s string) int {
149-
res := 0
150-
for _, c := range s {
151-
res = res*10 + int(c-'a')
152-
}
153-
return res
154-
}
155-
return f(firstWord)+f(secondWord) == f(targetWord)
161+
```ts
162+
function isSumEqual(
163+
firstWord: string,
164+
secondWord: string,
165+
targetWord: string,
166+
): boolean {
167+
const calc = (s: string) => {
168+
let res = 0;
169+
for (const c of s) {
170+
res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0);
171+
}
172+
return res;
173+
};
174+
return calc(firstWord) + calc(secondWord) === calc(targetWord);
175+
}
176+
177+
```
178+
179+
### **Rust**
180+
181+
```rust
182+
impl Solution {
183+
fn calc(s: &String) -> i32 {
184+
let mut res = 0;
185+
for c in s.as_bytes() {
186+
res = res * 10 + (c - b'a') as i32;
187+
}
188+
res
189+
}
190+
191+
pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
192+
Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word)
193+
}
194+
}
195+
```
196+
197+
### **C**
198+
199+
```c
200+
int calc(char *s) {
201+
int res = 0;
202+
for (int i = 0; s[i]; i++) {
203+
res = res * 10 + s[i] - 'a';
204+
}
205+
return res;
206+
}
207+
208+
bool isSumEqual(char *firstWord, char *secondWord, char *targetWord) {
209+
return calc(firstWord) + calc(secondWord) == calc(targetWord);
156210
}
157211
```
158212

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

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ public:
114114
};
115115
```
116116

117+
### **Go**
118+
119+
```go
120+
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
121+
f := func(s string) int {
122+
res := 0
123+
for _, c := range s {
124+
res = res*10 + int(c-'a')
125+
}
126+
return res
127+
}
128+
return f(firstWord)+f(secondWord) == f(targetWord)
129+
}
130+
```
131+
117132
### **JavaScript**
118133

119134
```js
@@ -135,18 +150,57 @@ var isSumEqual = function (firstWord, secondWord, targetWord) {
135150
};
136151
```
137152

138-
### **Go**
153+
### **TypeScript**
139154

140-
```go
141-
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
142-
f := func(s string) int {
143-
res := 0
144-
for _, c := range s {
145-
res = res*10 + int(c-'a')
146-
}
147-
return res
148-
}
149-
return f(firstWord)+f(secondWord) == f(targetWord)
155+
```ts
156+
function isSumEqual(
157+
firstWord: string,
158+
secondWord: string,
159+
targetWord: string,
160+
): boolean {
161+
const calc = (s: string) => {
162+
let res = 0;
163+
for (const c of s) {
164+
res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0);
165+
}
166+
return res;
167+
};
168+
return calc(firstWord) + calc(secondWord) === calc(targetWord);
169+
}
170+
171+
```
172+
173+
### **Rust**
174+
175+
```rust
176+
impl Solution {
177+
fn calc(s: &String) -> i32 {
178+
let mut res = 0;
179+
for c in s.as_bytes() {
180+
res = res * 10 + (c - b'a') as i32;
181+
}
182+
res
183+
}
184+
185+
pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
186+
Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word)
187+
}
188+
}
189+
```
190+
191+
### **C**
192+
193+
```c
194+
int calc(char *s) {
195+
int res = 0;
196+
for (int i = 0; s[i]; i++) {
197+
res = res * 10 + s[i] - 'a';
198+
}
199+
return res;
200+
}
201+
202+
bool isSumEqual(char *firstWord, char *secondWord, char *targetWord) {
203+
return calc(firstWord) + calc(secondWord) == calc(targetWord);
150204
}
151205
```
152206
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int calc(char *s) {
2+
int res = 0;
3+
for (int i = 0; s[i]; i++) {
4+
res = res * 10 + s[i] - 'a';
5+
}
6+
return res;
7+
}
8+
9+
bool isSumEqual(char *firstWord, char *secondWord, char *targetWord) {
10+
return calc(firstWord) + calc(secondWord) == calc(targetWord);
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
fn calc(s: &String) -> i32 {
3+
let mut res = 0;
4+
for c in s.as_bytes() {
5+
res = res * 10 + (c - b'a') as i32;
6+
}
7+
res
8+
}
9+
10+
pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
11+
Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word)
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function isSumEqual(
2+
firstWord: string,
3+
secondWord: string,
4+
targetWord: string,
5+
): boolean {
6+
const calc = (s: string) => {
7+
let res = 0;
8+
for (const c of s) {
9+
res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0);
10+
}
11+
return res;
12+
};
13+
return calc(firstWord) + calc(secondWord) === calc(targetWord);
14+
}

0 commit comments

Comments
 (0)