Skip to content

Commit 662de70

Browse files
committed
feat: add typescript solution to lc problem: No.1048.Longest String Chain
1 parent 119a500 commit 662de70

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

solution/1000-1099/1048.Longest String Chain/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ class Solution {
119119
}
120120
```
121121

122+
### **TypeScript**
123+
124+
```ts
125+
function longestStrChain(words: string[]): number {
126+
words.sort((a, b) => a.length - b.length);
127+
let ans = 0;
128+
let hashTable = new Map();
129+
for (let word of words) {
130+
let c = 1;
131+
for (let i = 0; i < word.length; i++) {
132+
let pre = word.substring(0, i) + word.substring(i+1);
133+
c = Math.max(c, (hashTable.get(pre) || 0) + 1);
134+
}
135+
hashTable.set(word, c);
136+
ans = Math.max(ans, c);
137+
}
138+
return ans;
139+
};
140+
```
141+
122142
### **C++**
123143

124144
哈希表:

solution/1000-1099/1048.Longest String Chain/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ class Solution {
108108
}
109109
```
110110

111+
### **TypeScript**
112+
113+
```ts
114+
function longestStrChain(words: string[]): number {
115+
words.sort((a, b) => a.length - b.length);
116+
let ans = 0;
117+
let hashTable = new Map();
118+
for (let word of words) {
119+
let c = 1;
120+
for (let i = 0; i < word.length; i++) {
121+
let pre = word.substring(0, i) + word.substring(i+1);
122+
c = Math.max(c, (hashTable.get(pre) || 0) + 1);
123+
}
124+
hashTable.set(word, c);
125+
ans = Math.max(ans, c);
126+
}
127+
return ans;
128+
};
129+
```
130+
111131
### **C++**
112132

113133
```cpp
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function longestStrChain(words: string[]): number {
2+
words.sort((a, b) => a.length - b.length);
3+
let ans = 0;
4+
let hashTable = new Map();
5+
for (let word of words) {
6+
let c = 1;
7+
for (let i = 0; i < word.length; i++) {
8+
let pre = word.substring(0, i) + word.substring(i+1);
9+
c = Math.max(c, (hashTable.get(pre) || 0) + 1);
10+
}
11+
hashTable.set(word, c);
12+
ans = Math.max(ans, c);
13+
}
14+
return ans;
15+
};

0 commit comments

Comments
 (0)