Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ceb60f

Browse files
authored
#3 Longest substring
1 parent 2eb4f3a commit 1ceb60f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

3-Longest-Substring.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
namespace AlgoPractice
3+
{
4+
public class Solution
5+
{
6+
public int LengthOfLongestSubstring(string s)
7+
{
8+
var set = new HashSet<char>();
9+
var maxLength = 0;
10+
var left = 0;
11+
var right = 0;
12+
while(right < s.Length)
13+
{
14+
var currentChar = s[right];
15+
while (set.Contains(currentChar) && left < s.Length)
16+
{
17+
set.Remove(s[left++]);
18+
}
19+
set.Add(currentChar);
20+
right++;
21+
var length = right - left;
22+
maxLength = Math.Max(length, maxLength);
23+
}
24+
return maxLength;
25+
}
26+
}
27+
}
28+

0 commit comments

Comments
 (0)