Skip to content

Commit 3e9c3ec

Browse files
committed
feat: add solutions to lc problem: No.1754
No.1754.Largest Merge Of Two Strings
1 parent 3d27848 commit 3e9c3ec

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

solution/1700-1799/1754.Largest Merge Of Two Strings/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,87 @@ func largestMerge(word1 string, word2 string) string {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function largestMerge(word1: string, word2: string): string {
162+
const m = word1.length;
163+
const n = word2.length;
164+
let ans = '';
165+
let i = 0;
166+
let j = 0;
167+
while (i < m && j < n) {
168+
ans += word1.slice(i) > word2.slice(j) ? word1[i++] : word2[j++];
169+
}
170+
ans += word1.slice(i);
171+
ans += word2.slice(j);
172+
return ans;
173+
}
174+
```
175+
176+
### **Rust**
177+
178+
```rust
179+
impl Solution {
180+
pub fn largest_merge(word1: String, word2: String) -> String {
181+
let word1 = word1.as_bytes();
182+
let word2 = word2.as_bytes();
183+
let m = word1.len();
184+
let n = word2.len();
185+
let mut ans = String::new();
186+
let mut i = 0;
187+
let mut j = 0;
188+
while i < m && j < n {
189+
if word1[i..] > word2[j..] {
190+
ans.push(word1[i] as char);
191+
i += 1;
192+
} else {
193+
ans.push(word2[j] as char);
194+
j += 1;
195+
}
196+
}
197+
word1[i..].iter().for_each(|c| ans.push(*c as char));
198+
word2[j..].iter().for_each(|c| ans.push(*c as char));
199+
ans
200+
}
201+
}
202+
```
203+
204+
### **C**
205+
206+
```c
207+
char *largestMerge(char *word1, char *word2) {
208+
int m = strlen(word1);
209+
int n = strlen(word2);
210+
int i = 0;
211+
int j = 0;
212+
char *ans = malloc((m + n + 1) * sizeof(char));
213+
while (i < m && j < n) {
214+
int k = 0;
215+
while (word1[i + k] && word2[j + k] && word1[i + k] == word2[j + k]) {
216+
k++;
217+
}
218+
if (word1[i + k] > word2[j + k]) {
219+
ans[i + j] = word1[i];
220+
i++;
221+
} else {
222+
ans[i + j] = word2[j];
223+
j++;
224+
};
225+
}
226+
while (word1[i]) {
227+
ans[i + j] = word1[i];
228+
i++;
229+
}
230+
while (word2[j]) {
231+
ans[i + j] = word2[j];
232+
j++;
233+
}
234+
ans[m + n] = '\0';
235+
return ans;
236+
}
237+
```
238+
158239
### **...**
159240
160241
```

solution/1700-1799/1754.Largest Merge Of Two Strings/README_EN.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,87 @@ func largestMerge(word1 string, word2 string) string {
137137
}
138138
```
139139

140+
### **TypeScript**
141+
142+
```ts
143+
function largestMerge(word1: string, word2: string): string {
144+
const m = word1.length;
145+
const n = word2.length;
146+
let ans = '';
147+
let i = 0;
148+
let j = 0;
149+
while (i < m && j < n) {
150+
ans += word1.slice(i) > word2.slice(j) ? word1[i++] : word2[j++];
151+
}
152+
ans += word1.slice(i);
153+
ans += word2.slice(j);
154+
return ans;
155+
}
156+
```
157+
158+
### **Rust**
159+
160+
```rust
161+
impl Solution {
162+
pub fn largest_merge(word1: String, word2: String) -> String {
163+
let word1 = word1.as_bytes();
164+
let word2 = word2.as_bytes();
165+
let m = word1.len();
166+
let n = word2.len();
167+
let mut ans = String::new();
168+
let mut i = 0;
169+
let mut j = 0;
170+
while i < m && j < n {
171+
if word1[i..] > word2[j..] {
172+
ans.push(word1[i] as char);
173+
i += 1;
174+
} else {
175+
ans.push(word2[j] as char);
176+
j += 1;
177+
}
178+
}
179+
word1[i..].iter().for_each(|c| ans.push(*c as char));
180+
word2[j..].iter().for_each(|c| ans.push(*c as char));
181+
ans
182+
}
183+
}
184+
```
185+
186+
### **C**
187+
188+
```c
189+
char *largestMerge(char *word1, char *word2) {
190+
int m = strlen(word1);
191+
int n = strlen(word2);
192+
int i = 0;
193+
int j = 0;
194+
char *ans = malloc((m + n + 1) * sizeof(char));
195+
while (i < m && j < n) {
196+
int k = 0;
197+
while (word1[i + k] && word2[j + k] && word1[i + k] == word2[j + k]) {
198+
k++;
199+
}
200+
if (word1[i + k] > word2[j + k]) {
201+
ans[i + j] = word1[i];
202+
i++;
203+
} else {
204+
ans[i + j] = word2[j];
205+
j++;
206+
};
207+
}
208+
while (word1[i]) {
209+
ans[i + j] = word1[i];
210+
i++;
211+
}
212+
while (word2[j]) {
213+
ans[i + j] = word2[j];
214+
j++;
215+
}
216+
ans[m + n] = '\0';
217+
return ans;
218+
}
219+
```
220+
140221
### **...**
141222
142223
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
char *largestMerge(char *word1, char *word2) {
2+
int m = strlen(word1);
3+
int n = strlen(word2);
4+
int i = 0;
5+
int j = 0;
6+
char *ans = malloc((m + n + 1) * sizeof(char));
7+
while (i < m && j < n) {
8+
int k = 0;
9+
while (word1[i + k] && word2[j + k] && word1[i + k] == word2[j + k]) {
10+
k++;
11+
}
12+
if (word1[i + k] > word2[j + k]) {
13+
ans[i + j] = word1[i];
14+
i++;
15+
} else {
16+
ans[i + j] = word2[j];
17+
j++;
18+
};
19+
}
20+
while (word1[i]) {
21+
ans[i + j] = word1[i];
22+
i++;
23+
}
24+
while (word2[j]) {
25+
ans[i + j] = word2[j];
26+
j++;
27+
}
28+
ans[m + n] = '\0';
29+
return ans;
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn largest_merge(word1: String, word2: String) -> String {
3+
let word1 = word1.as_bytes();
4+
let word2 = word2.as_bytes();
5+
let m = word1.len();
6+
let n = word2.len();
7+
let mut ans = String::new();
8+
let mut i = 0;
9+
let mut j = 0;
10+
while i < m && j < n {
11+
if word1[i..] > word2[j..] {
12+
ans.push(word1[i] as char);
13+
i += 1;
14+
} else {
15+
ans.push(word2[j] as char);
16+
j += 1;
17+
}
18+
}
19+
word1[i..].iter().for_each(|c| ans.push(*c as char));
20+
word2[j..].iter().for_each(|c| ans.push(*c as char));
21+
ans
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function largestMerge(word1: string, word2: string): string {
2+
const m = word1.length;
3+
const n = word2.length;
4+
let ans = '';
5+
let i = 0;
6+
let j = 0;
7+
while (i < m && j < n) {
8+
ans += word1.slice(i) > word2.slice(j) ? word1[i++] : word2[j++];
9+
}
10+
ans += word1.slice(i);
11+
ans += word2.slice(j);
12+
return ans;
13+
}

0 commit comments

Comments
 (0)