Skip to content

Commit ad912fb

Browse files
committed
Added additional solution to csharp/0003-longest-substring-without-repeating-characters.cs
1 parent 9014b37 commit ad912fb

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
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
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy