33
44use std:: collections:: VecDeque ;
55
6- pub fn length_of_longest_substring ( s : String ) -> usize {
7- let array = s. as_bytes ( ) ;
8- let mut c: VecDeque < u8 > = VecDeque :: with_capacity ( array . len ( ) ) ;
6+ pub fn length_of_longest_substring ( s : & str ) -> usize {
7+ let array: Vec < ( char ) > = s. char_indices ( ) . map ( |i| i . 1 ) . collect :: < Vec < _ > > ( ) ;
8+ let mut c: VecDeque < ( char ) > = VecDeque :: with_capacity ( s . char_indices ( ) . count ( ) ) ;
99 let mut max: usize = 0 ;
1010
1111 for x in array. iter ( ) {
1212 if c. contains ( x) {
13- let mut z: Option < u8 > = c. pop_front ( ) ;
13+ let mut z: Option < char > = c. pop_front ( ) ;
1414 while z != Some ( * x) {
1515 z = c. pop_front ( ) ;
1616 }
@@ -35,9 +35,9 @@ pub fn length_of_longest_substring(s: String) -> usize {
3535
3636use std:: collections:: HashMap ;
3737
38- pub fn approach_two ( s : String ) -> usize {
39- let array = s. as_bytes ( ) ;
40- let mut reviews: HashMap < & u8 , usize > = HashMap :: with_capacity ( array . len ( ) ) ;
38+ pub fn approach_two ( s : & str ) -> usize {
39+ let array: Vec < ( char ) > = s. char_indices ( ) . map ( |i| i . 1 ) . collect :: < Vec < _ > > ( ) ;
40+ let mut reviews: HashMap < & char , usize > = HashMap :: with_capacity ( s . char_indices ( ) . count ( ) ) ;
4141 let mut i: usize = 0 ;
4242 let mut index: usize = 0 ;
4343 let mut max: usize = 0 ;
@@ -62,54 +62,58 @@ mod test {
6262
6363 #[ test]
6464 fn test_length_of_longest_substring ( ) {
65- assert_eq ! ( length_of_longest_substring( String :: from ( "world" ) ) , 5 ) ;
65+ assert_eq ! ( length_of_longest_substring( "world" ) , 5 ) ;
6666
67- assert_eq ! ( length_of_longest_substring( String :: from ( "hello world" ) ) , 6 ) ;
67+ assert_eq ! ( length_of_longest_substring( "hello world" ) , 6 ) ;
6868
69- assert_eq ! ( length_of_longest_substring( String :: from ( "helloworld" ) ) , 5 ) ;
69+ assert_eq ! ( length_of_longest_substring( "helloworld" ) , 5 ) ;
7070
71- assert_eq ! ( length_of_longest_substring( String :: from ( "abcabcbb" ) ) , 3 ) ;
71+ assert_eq ! ( length_of_longest_substring( "abcabcbb" ) , 3 ) ;
7272
73- assert_eq ! ( length_of_longest_substring( String :: from ( "bbbbb" ) ) , 1 ) ;
73+ assert_eq ! ( length_of_longest_substring( "bbbbb" ) , 1 ) ;
7474
75- assert_eq ! ( length_of_longest_substring( String :: from ( "pwwkew" ) ) , 3 ) ;
75+ assert_eq ! ( length_of_longest_substring( "pwwkew" ) , 3 ) ;
7676
77- assert_eq ! ( length_of_longest_substring( String :: from ( "b" ) ) , 1 ) ;
77+ assert_eq ! ( length_of_longest_substring( "b" ) , 1 ) ;
7878
79- assert_eq ! ( length_of_longest_substring( String :: from( "" ) ) , 0 ) ;
79+ assert_eq ! ( length_of_longest_substring( "" ) , 0 ) ;
80+
81+ assert_eq ! ( length_of_longest_substring( "大大小小" ) , 2 ) ;
8082 }
8183
8284 use super :: approach_two;
8385
8486 #[ test]
8587 fn test_approach_two ( ) {
86- assert_eq ! ( approach_two( String :: from( "world" ) ) , 5 ) ;
88+ assert_eq ! ( approach_two( "world" ) , 5 ) ;
89+
90+ assert_eq ! ( approach_two( "hello world" ) , 6 ) ;
8791
88- assert_eq ! ( approach_two( String :: from ( "hello world" ) ) , 6 ) ;
92+ assert_eq ! ( approach_two( "helloworld" ) , 5 ) ;
8993
90- assert_eq ! ( approach_two( String :: from ( "helloworld" ) ) , 5 ) ;
94+ assert_eq ! ( approach_two( "abcabcbb" ) , 3 ) ;
9195
92- assert_eq ! ( approach_two( String :: from ( "abcabcbb" ) ) , 3 ) ;
96+ assert_eq ! ( approach_two( "bbbbb" ) , 1 ) ;
9397
94- assert_eq ! ( approach_two( String :: from ( "bbbbb" ) ) , 1 ) ;
98+ assert_eq ! ( approach_two( "pwwkew" ) , 3 ) ;
9599
96- assert_eq ! ( approach_two( String :: from ( "pwwkew" ) ) , 3 ) ;
100+ assert_eq ! ( approach_two( "b" ) , 1 ) ;
97101
98- assert_eq ! ( approach_two( String :: from ( "b" ) ) , 1 ) ;
102+ assert_eq ! ( approach_two( "" ) , 0 ) ;
99103
100- assert_eq ! ( approach_two( String :: from ( "" ) ) , 0 ) ;
104+ assert_eq ! ( approach_two( "大大小小" ) , 2 ) ;
101105 }
102106
103107 use test:: Bencher ;
104108 // 96 ns/iter (+/- 26)
105109 #[ bench]
106110 fn bench_length_of_longest_substring ( b : & mut Bencher ) {
107- b. iter ( || length_of_longest_substring ( String :: from ( "hello world" ) ) ) ;
111+ b. iter ( || length_of_longest_substring ( "hello world" ) ) ;
108112 }
109113
110114 // 561 ns/iter (+/- 180)
111115 #[ bench]
112116 fn bench_approach_two ( b : & mut Bencher ) {
113- b. iter ( || approach_two ( String :: from ( "hello world" ) ) ) ;
117+ b. iter ( || approach_two ( "hello world" ) ) ;
114118 }
115119}
0 commit comments