File tree Expand file tree Collapse file tree 1 file changed +47
-5
lines changed
Expand file tree Collapse file tree 1 file changed +47
-5
lines changed Original file line number Diff line number Diff line change 1- public class Solution {
2- public int LengthOfLongestSubstring ( string s ) {
1+ // hashset
2+ public class Solution
3+ {
4+ public int LengthOfLongestSubstring ( string s )
5+ {
36 int leftPointer = 0 , rightPointer = 0 , maxLength = 0 ;
47 HashSet < int > chars = new HashSet < int > ( ) ;
58
6- while ( rightPointer < s . Length ) {
9+ while ( rightPointer < s . Length )
10+ {
711 char currChar = s [ rightPointer ] ;
8- if ( chars . Contains ( currChar ) ) { // Move left pointer until all duplicate chars removed
12+ if ( chars . Contains ( currChar ) )
13+ {
14+ // Move left pointer until all duplicate chars removed
915 chars . Remove ( s [ leftPointer ] ) ;
1016 leftPointer ++ ;
11- } else {
17+ }
18+ else
19+ {
1220 chars . Add ( currChar ) ;
1321 maxLength = Math . Max ( maxLength , rightPointer - leftPointer + 1 ) ;
1422 rightPointer ++ ;
1523 }
1624 }
1725 return maxLength ;
1826 }
27+ }
28+
29+ //bitmask
30+ public class Solution
31+ {
32+ private Int128 ONE = 1 ;
33+ public int LengthOfLongestSubstring ( string s )
34+ {
35+ int Convert ( char ch ) => ch - ' ' ;
36+ Int128 mask = 0 ;
37+ int l = 0 , r = 0 , output = 0 ;
38+ while ( r < s . Length )
39+ {
40+ Int128 temp = mask ^ ( ONE << Convert ( s [ r ] ) ) ;
41+ if ( temp < mask )
42+ {
43+ while ( s [ l ] != s [ r ] )
44+ {
45+ mask ^= ONE << Convert ( s [ l ] ) ;
46+ l ++ ;
47+ }
48+ mask ^= ONE << Convert ( s [ l ] ) ;
49+ l ++ ;
50+ }
51+ else
52+ {
53+ mask = temp ;
54+ output = Math . Max ( output , r - l + 1 ) ;
55+ r ++ ;
56+ }
57+ }
58+
59+ return output ;
60+ }
1961}
You can’t perform that action at this time.
0 commit comments