File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
solution/1000-1099/1048.Longest String Chain Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,26 @@ class Solution {
119
119
}
120
120
```
121
121
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
+
122
142
### ** C++**
123
143
124
144
哈希表:
Original file line number Diff line number Diff line change @@ -108,6 +108,26 @@ class Solution {
108
108
}
109
109
```
110
110
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
+
111
131
### ** C++**
112
132
113
133
``` cpp
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments