Skip to content

Instantly share code, notes, and snippets.

@linnet8989
Created February 22, 2017 07:45
Show Gist options
  • Save linnet8989/a9873762ab15b1f0899382e147183dcf to your computer and use it in GitHub Desktop.
Save linnet8989/a9873762ab15b1f0899382e147183dcf to your computer and use it in GitHub Desktop.
3. Longest Substring Without Repeating Characters
public class Solution
{
public int LengthOfLongestSubstring(string s)
{
int subLength = 0;
int r = 0;
int repetitionIndex;
int start = 0;
for (int i = 0; i < s.Length; i++)
{
repetitionIndex = s.Substring(start, i - start).LastIndexOf(s[i]);
if (repetitionIndex > -1)
{
repetitionIndex += start;
}
if (subLength >= i - repetitionIndex)
{
subLength = i - repetitionIndex;
start = repetitionIndex + 1;
}
else
{
subLength += 1;
if (r < subLength)
{
r = subLength;
}
}
}
return r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment