Skip to content

Commit 2aa67b7

Browse files
committedMar 12, 2022
feat: add solutions to lc problems: No.0524
No.0524.Longest Word in Dictionary through Deleting
1 parent 1411a1d commit 2aa67b7

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
 

Diff for: ‎solution/0500-0599/0524.Longest Word in Dictionary through Deleting/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,65 @@
5959

6060
```
6161

62+
### **TypeScript**
63+
64+
```ts
65+
function findLongestWord(s: string, dictionary: string[]): string {
66+
dictionary.sort((a, b) => {
67+
if (a.length === b.length) {
68+
return b < a ? 1 : -1;
69+
}
70+
return b.length - a.length;
71+
});
72+
const n = s.length;
73+
for (const target of dictionary) {
74+
const m = target.length;
75+
if (m > n) {
76+
continue;
77+
}
78+
let i = 0;
79+
let j = 0;
80+
while (i < n && j < m) {
81+
if (s[i] === target[j]) {
82+
j++;
83+
}
84+
i++;
85+
}
86+
if (j === m) {
87+
return target;
88+
}
89+
}
90+
return '';
91+
}
92+
```
93+
94+
### **Rust**
95+
96+
```rust
97+
impl Solution {
98+
pub fn find_longest_word(s: String, mut dictionary: Vec<String>) -> String {
99+
dictionary.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
100+
for target in dictionary {
101+
let target: Vec<char> = target.chars().collect();
102+
let n = target.len();
103+
let mut i = 0;
104+
for c in s.chars() {
105+
if i == n {
106+
break;
107+
}
108+
if c == target[i] {
109+
i += 1;
110+
}
111+
}
112+
if i == n {
113+
return target.iter().collect();
114+
}
115+
}
116+
String::new()
117+
}
118+
}
119+
```
120+
62121
### **...**
63122

64123
```

Diff for: ‎solution/0500-0599/0524.Longest Word in Dictionary through Deleting/README_EN.md

+59
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,65 @@
4747

4848
```
4949

50+
### **TypeScript**
51+
52+
```ts
53+
function findLongestWord(s: string, dictionary: string[]): string {
54+
dictionary.sort((a, b) => {
55+
if (a.length === b.length) {
56+
return b < a ? 1 : -1;
57+
}
58+
return b.length - a.length;
59+
});
60+
const n = s.length;
61+
for (const target of dictionary) {
62+
const m = target.length;
63+
if (m > n) {
64+
continue;
65+
}
66+
let i = 0;
67+
let j = 0;
68+
while (i < n && j < m) {
69+
if (s[i] === target[j]) {
70+
j++;
71+
}
72+
i++;
73+
}
74+
if (j === m) {
75+
return target;
76+
}
77+
}
78+
return '';
79+
}
80+
```
81+
82+
### **Rust**
83+
84+
```rust
85+
impl Solution {
86+
pub fn find_longest_word(s: String, mut dictionary: Vec<String>) -> String {
87+
dictionary.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
88+
for target in dictionary {
89+
let target: Vec<char> = target.chars().collect();
90+
let n = target.len();
91+
let mut i = 0;
92+
for c in s.chars() {
93+
if i == n {
94+
break;
95+
}
96+
if c == target[i] {
97+
i += 1;
98+
}
99+
}
100+
if i == n {
101+
return target.iter().collect();
102+
}
103+
}
104+
String::new()
105+
}
106+
}
107+
```
108+
50109
### **...**
51110

52111
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn find_longest_word(s: String, mut dictionary: Vec<String>) -> String {
3+
dictionary.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
4+
for target in dictionary {
5+
let target: Vec<char> = target.chars().collect();
6+
let n = target.len();
7+
let mut i = 0;
8+
for c in s.chars() {
9+
if i == n {
10+
break;
11+
}
12+
if c == target[i] {
13+
i += 1;
14+
}
15+
}
16+
if i == n {
17+
return target.iter().collect();
18+
}
19+
}
20+
String::new()
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function findLongestWord(s: string, dictionary: string[]): string {
2+
dictionary.sort((a, b) => {
3+
if (a.length === b.length) {
4+
return b < a ? 1 : -1;
5+
}
6+
return b.length - a.length;
7+
});
8+
const n = s.length;
9+
for (const target of dictionary) {
10+
const m = target.length;
11+
if (m > n) {
12+
continue;
13+
}
14+
let i = 0;
15+
let j = 0;
16+
while (i < n && j < m) {
17+
if (s[i] === target[j]) {
18+
j++;
19+
}
20+
i++;
21+
}
22+
if (j === m) {
23+
return target;
24+
}
25+
}
26+
return '';
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.