Skip to content

Commit 47270a9

Browse files
authored
Merge pull request #11 from rickrain/optimizations
code optimizations
2 parents 701a57a + 049c3da commit 47270a9

File tree

1 file changed

+3
-11
lines changed
  • src/prob_0003_longest_substring

1 file changed

+3
-11
lines changed

src/prob_0003_longest_substring/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@ impl Solution {
77
return 0;
88
}
99

10-
let mut chars = s.char_indices();
11-
chars.next();
1210
let mut longest_substr = 1;
1311
let mut curr_substr_start_idx = 0;
1412
let mut curr_substr = &s[..=0];
1513

16-
for (idx, c) in chars {
14+
for (idx, c) in s.char_indices() {
1715
if curr_substr.contains(c) {
18-
if curr_substr.len() > longest_substr {
19-
longest_substr = curr_substr.len();
20-
}
21-
2216
while curr_substr_start_idx < idx {
2317
if s.as_bytes()[curr_substr_start_idx] as char == c {
2418
curr_substr_start_idx += 1;
@@ -27,11 +21,9 @@ impl Solution {
2721
curr_substr_start_idx += 1;
2822
}
2923
}
24+
3025
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());
3527
}
3628

3729
longest_substr as i32

0 commit comments

Comments
 (0)