C# solution using sliding window


  • 0
    A
    public class Solution {
        public int LengthOfLongestSubstring(string s) {
            var set = new Dictionary<char, int>();
            var ret = 0;
            for (int l = 0, r = 0; r < s.Length; r++){
                if (set.ContainsKey(s[r])) {
                    l = Math.Max(l, set[s[r]] + 1);
                }
                set[s[r]] = r;
                ret = Math.Max(ret, r - l + 1);
            }
            return ret;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.