Sliding window

https://medium.com/leetcode-patterns/leetcode-pattern-2-sliding-windows-for-strings-e19af105316b

https://www.jianshu.com/p/f965b12e3b8e

Longest Substring Without Repeating Characters

http://www.noteanddata.com/leetcode-3-Longest-Substring-Without-Repeating-Characters-java-sliding-windows-solution-note.html

public class Solution {
    public int LengthOfLongestSubstring(string s) {
        int max = 0;

        if(string.IsNullOrEmpty(s)){
            return 0;
        }

        for(int i = 0 ; i < s.Length; i++){
            Dictionary<char, int> cache = new Dictionary<char, int>();

            for(int j = i; j < s.Length; j++){
                if(cache.ContainsKey(s[j])){
                    i = cache[s[j]]; //jump
                    break;
                }
                else{
                    cache.Add(s[j], j);
                    max = Math.Max(max, cache.Count);
                }
            }

        }
        return max;
    }
}

Last updated

Was this helpful?