Created
February 22, 2017 07:45
-
-
Save linnet8989/a9873762ab15b1f0899382e147183dcf to your computer and use it in GitHub Desktop.
3. Longest Substring Without Repeating Characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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