problem 3
This commit is contained in:
parent
35a2375f7e
commit
99a222c319
30
LengthOfLongestSubstring.cs
Normal file
30
LengthOfLongestSubstring.cs
Normal file
@ -0,0 +1,30 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user