Programing/Algorithm
[LeetCode] 3. Longest Substring Without Repeating Characters
Napoliano
2025. 1. 20. 22:48
728x90
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
public class Solution {
public int LengthOfLongestSubstring(string s)
{
int anwser = 0;
int startIndex = 0;
var set = new HashSet<char>();
for (int i = 0; i < s.Length; i++)
{
var c = s[i];
if (set.Contains(c))
{
anwser = Math.Max(anwser, set.Count);
set.Remove(s[startIndex++]);
--i;
}
else
{
set.Add(c);
}
}
return Math.Max(anwser, set.Count);
}
}
728x90