In the Leetcode Longest Substring Without Repeating Characters problem solution in C# programming Given a string s, find the length of the longest substring without repeating characters.
Leetcode Longest Substring Without Repeating Characters problem solution in C# programming
public class Solution {
public int LengthOfLongestSubstring(string s) {
if(s.Length==0)
{
return 0;
}
HashSet<char> set = new HashSet<char>();
int count = 0;
int max = 0;
for (int i = 0; i < s.Length; i++)
{
count = 0;
for (int j = i; j < s.Length; j++)
{
if (!set.Contains(s[j]))
{
set.Add(s[j]);
count++;
}
else
{
set.Clear();
break;
}
}
if(max<count)
{
max = count;
}
}
return max;
}
}
Also read,
- Leetcode Longest Substring Without Repeating Characters problem solution in C
- Leetcode Longest Substring Without Repeating Characters problem solution in C++
- Leetcode Longest Substring Without Repeating Characters problem solution in Java
- Leetcode Longest Substring Without Repeating Characters problem solution in Python