Skip to content

Commit 4927623

Browse files
committed
feat: add solutions to lc problem: No.0720
No.0720.Longest Word in Dictionary
1 parent 35e5f8b commit 4927623

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

solution/0700-0799/0720.Longest Word in Dictionary/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,57 @@ words = ["a", "banana", "app", "appl", &
6262

6363
```
6464

65+
### **TypeScript**
66+
67+
```ts
68+
function longestWord(words: string[]): string {
69+
words.sort((a, b) => {
70+
const n = a.length;
71+
const m = b.length;
72+
if (n === m) {
73+
return a < b ? -1 : 1;
74+
}
75+
return m - n;
76+
});
77+
for (const word of words) {
78+
let isPass = true;
79+
for (let i = 1; i <= word.length; i++) {
80+
if (!words.includes(word.slice(0, i))) {
81+
isPass = false;
82+
break;
83+
}
84+
}
85+
if (isPass) {
86+
return word;
87+
}
88+
}
89+
return '';
90+
}
91+
```
92+
93+
### **Rust**
94+
95+
```rust
96+
impl Solution {
97+
pub fn longest_word(mut words: Vec<String>) -> String {
98+
words.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
99+
for word in words.iter() {
100+
let mut is_pass = true;
101+
for i in 1..=word.len() {
102+
if !words.contains(&word[..i].to_string()) {
103+
is_pass = false;
104+
break;
105+
}
106+
}
107+
if is_pass {
108+
return word.clone();
109+
}
110+
}
111+
String::new()
112+
}
113+
}
114+
```
115+
65116
### **...**
66117

67118
```

solution/0700-0799/0720.Longest Word in Dictionary/README_EN.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,57 @@
5050

5151
```
5252

53+
### **TypeScript**
54+
55+
```ts
56+
function longestWord(words: string[]): string {
57+
words.sort((a, b) => {
58+
const n = a.length;
59+
const m = b.length;
60+
if (n === m) {
61+
return a < b ? -1 : 1;
62+
}
63+
return m - n;
64+
});
65+
for (const word of words) {
66+
let isPass = true;
67+
for (let i = 1; i <= word.length; i++) {
68+
if (!words.includes(word.slice(0, i))) {
69+
isPass = false;
70+
break;
71+
}
72+
}
73+
if (isPass) {
74+
return word;
75+
}
76+
}
77+
return '';
78+
}
79+
```
80+
81+
### **Rust**
82+
83+
```rust
84+
impl Solution {
85+
pub fn longest_word(mut words: Vec<String>) -> String {
86+
words.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
87+
for word in words.iter() {
88+
let mut is_pass = true;
89+
for i in 1..=word.len() {
90+
if !words.contains(&word[..i].to_string()) {
91+
is_pass = false;
92+
break;
93+
}
94+
}
95+
if is_pass {
96+
return word.clone();
97+
}
98+
}
99+
String::new()
100+
}
101+
}
102+
```
103+
53104
### **...**
54105

55106
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn longest_word(mut words: Vec<String>) -> String {
3+
words.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
4+
for word in words.iter() {
5+
let mut is_pass = true;
6+
for i in 1..=word.len() {
7+
if !words.contains(&word[..i].to_string()) {
8+
is_pass = false;
9+
break;
10+
}
11+
}
12+
if is_pass {
13+
return word.clone();
14+
}
15+
}
16+
String::new()
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function longestWord(words: string[]): string {
2+
words.sort((a, b) => {
3+
const n = a.length;
4+
const m = b.length;
5+
if (n === m) {
6+
return a < b ? -1 : 1;
7+
}
8+
return m - n;
9+
});
10+
for (const word of words) {
11+
let isPass = true;
12+
for (let i = 1; i <= word.length; i++) {
13+
if (!words.includes(word.slice(0, i))) {
14+
isPass = false;
15+
break;
16+
}
17+
}
18+
if (isPass) {
19+
return word;
20+
}
21+
}
22+
return '';
23+
}

0 commit comments

Comments
 (0)