File tree Expand file tree Collapse file tree 1 file changed +3
-11
lines changed
src/prob_0003_longest_substring Expand file tree Collapse file tree 1 file changed +3
-11
lines changed Original file line number Diff line number Diff line change @@ -7,18 +7,12 @@ impl Solution {
7
7
return 0 ;
8
8
}
9
9
10
- let mut chars = s. char_indices ( ) ;
11
- chars. next ( ) ;
12
10
let mut longest_substr = 1 ;
13
11
let mut curr_substr_start_idx = 0 ;
14
12
let mut curr_substr = & s[ ..=0 ] ;
15
13
16
- for ( idx, c) in chars {
14
+ for ( idx, c) in s . char_indices ( ) {
17
15
if curr_substr. contains ( c) {
18
- if curr_substr. len ( ) > longest_substr {
19
- longest_substr = curr_substr. len ( ) ;
20
- }
21
-
22
16
while curr_substr_start_idx < idx {
23
17
if s. as_bytes ( ) [ curr_substr_start_idx] as char == c {
24
18
curr_substr_start_idx += 1 ;
@@ -27,11 +21,9 @@ impl Solution {
27
21
curr_substr_start_idx += 1 ;
28
22
}
29
23
}
24
+
30
25
curr_substr = & s[ curr_substr_start_idx..=idx] ;
31
- }
32
-
33
- if curr_substr. len ( ) > longest_substr {
34
- longest_substr = curr_substr. len ( ) ;
26
+ longest_substr = longest_substr. max ( curr_substr. len ( ) ) ;
35
27
}
36
28
37
29
longest_substr as i32
You can’t perform that action at this time.
0 commit comments