LeetCode/LengthOfLongestSubstring.cs
2022-04-14 13:35:04 +02:00

30 lines
680 B
C#

public class Solution {
public int LengthOfLongestSubstring(string s) {
int max = 0;
int n = 0;
for (int i = 0; i < s.Length; i++)
{
n++;
if (n > 1)
{
n -= FindDuplicateCharacter(s.Substring(i - n + 1, n)) + 1;
}
if (n > max) {
max = n;
}
}
return max;
}
public int FindDuplicateCharacter(string s) {
char last = s[s.Length - 1];
for (int i = 0; i < s.Length - 1; i++)
{
if (s[i] == last)
{
return i;
}
}
return -1;
}
}