C# time limited exceed


  • 0
    Y

    //test cases:
    //a, aa, ab,aba, abb, aab, abcabcbb

    public class Solution {
    public int LengthOfLongestSubstring(string s) {

      if(string.IsNullOrEmpty(s))
          return 0;
        if(s.Length==1)
          return 1;
      
      var mainHash=new Dictionary<char,int>();
      //var tempHash=new Dictionary<char,int>();
      var i = 0;
      int lowIndex = 0;
      int stringLength = 1;
      int lowMainHash=i;
      int j = i + 1;
      var indexes = new List<char>();
      
      mainHash.Add(s[i],i);
      
      for(; j < s.Length; j++)
      {
          if(mainHash.ContainsKey(s[j]))
          {
              if(j - i > stringLength)
              {
                  stringLength = j - i;
                  lowIndex = i;
              }
              
              i = mainHash[s[j]] + 1;
              
              lowMainHash = i;
              
              indexes = new List<char>();
              
              foreach(var item in mainHash)
              {
                  if(item.Value < i)
                    indexes.Add(item.Key);
              }
              
              foreach(var item in indexes)
              {
                    mainHash.Remove(item);
              }
          }
            mainHash.Add(s[j],j);
      }
      
      if(mainHash.Count > stringLength)
      {
          lowIndex = lowMainHash;
          stringLength = mainHash.Count;
      }
      
      return stringLength;
        
    }
    

    }

    //test cases:
    //a, aa, ab,aba, abb, aab, abcabcbb

    public class Solution {
    public int LengthOfLongestSubstring(string s) {

      if(string.IsNullOrEmpty(s))
          return 0;
        if(s.Length==1)
          return 1;
          
    
    var mainHash=new HashSet<char>();
      //var tempHash=new Dictionary<char,int>();
      var i = 0;
      int stringLength = 1;
      int j = i + 1;
      
      mainHash.Add(s[i]);
      
      while(j < s.Length)
      {
          if(mainHash.Contains(s[j]))
          {
              mainHash.Remove(s[i++]);
          }
          else
          {
             mainHash.Add(s[j++]);
             stringLength = Math.Max(stringLength,mainHash.Count);
          }
      }
      
      return stringLength;
          
    
    
          
      
    
    
      
      
        
    }
    

    }

    if(string.IsNullOrEmpty(s))
    return 0;
    if(s.Length==1)
    return 1;

    var mainHash=new HashSet<char>();
      //var tempHash=new Dictionary<char,int>();
      var i = 0;
      int stringLength = 1;
      int j = i + 1;
      
      mainHash.Add(s[i]);
      
      for(;j<s.Length;j++)
      {
              while(mainHash.Contains(s[j]))
              {
                  mainHash.Remove(s[i]);
                  i = i+1;
              }
          
            mainHash.Add(s[j]);
            stringLength = Math.Max(stringLength,mainHash.Count);
      }
      
      return stringLength;

Log in to reply
 

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