C# solution using list


  • 0
    K
    public class Solution {
        public int LengthOfLongestSubstring(string s) {
            
            int maxlen = 0;
            int j = 0;
            int m = 0;
            List<char> list = new List<char>();
            
            for (int i=0;i<s.Length;i++) {
                if (!list.Contains(s[i])) {
                    list.Add(s[i]);
                    j++;
                    if (j > maxlen) maxlen = j;
                }
                else {
                    m = list.IndexOf(s[i]);
                    for (int k =0;k<=m;k++) {
                        list.RemoveAt(0);
                        j--;
                    }
                    list.Add(s[i]);
                    j++;
                }
            }
            return maxlen;
        }
    }
    

Log in to reply
 

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