Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public int LengthOfLongestSubstring(string s) {
- Dictionary<char,int> charToPositionMap = new Dictionary<char,int>();
- int currentStart = 0;
- int longestSubstringLength = 0;
- int currentSubstringLength = 0;
- while(currentStart < s.Length)
- {
- bool duplicated = false;
- for(int i = currentStart; i < s.Length; i++)
- {
- if(!charToPositionMap.ContainsKey(s[i]))
- {
- charToPositionMap.Add(s[i],i);
- currentSubstringLength++;
- }
- else
- {
- if(currentSubstringLength > longestSubstringLength) longestSubstringLength = currentSubstringLength;
- currentStart = charToPositionMap[s[i]] + 1;
- charToPositionMap.Clear();
- currentSubstringLength = 0;
- duplicated = true;
- break;
- }
- }
- if (currentSubstringLength > longestSubstringLength) longestSubstringLength = currentSubstringLength;
- if(!duplicated) currentStart = s.Length;
- }
- return longestSubstringLength;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement