knakul853

Untitled

Jul 15th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6. public:
  7.     int lengthOfLongestSubstring(string s) {
  8.        
  9.         int n = (int)s.size();
  10.         vector<int>dict(256, 0);
  11.         int ans = INT_MIN;
  12.        
  13.          int i=0, j=0;
  14.        
  15.         while(i < n )
  16.         {
  17.             char c = s[i++];
  18.            
  19.             dict[c]++;
  20.            
  21.             while( j < i && dict[c] > 1 )
  22.             {
  23.                 dict[s[j]]--;
  24.                 j++;
  25.             }
  26.            
  27.             ans = max( ans, i - j );      
  28.            
  29.         }
  30.        
  31.         if(ans == INT_MIN) return 0;;
  32.         return ans;
  33.     }
  34. };
Add Comment
Please, Sign In to add comment