File tree 4 files changed +167
-0
lines changed
solution/0500-0599/0524.Longest Word in Dictionary through Deleting
4 files changed +167
-0
lines changed Original file line number Diff line number Diff line change 59
59
60
60
```
61
61
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
+
62
121
### ** ...**
63
122
64
123
```
Original file line number Diff line number Diff line change 47
47
48
48
```
49
49
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
+
50
109
### ** ...**
51
110
52
111
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments