1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class Solution { public: int lengthOfLongestSubstring(string s) { int cnt=s.size(),sum=0; map<char,int>mp; for(int i=0;i<cnt;i++){ if(!mp.count(s[i])){ sum++; mp[s[i]]=1; } } if(sum==1)return 1; if(sum==2)return 2; if(sum==cnt)return sum;
for(int i=sum;i>=3;i--){ for(int j=0;j<=cnt-i;j++){ map<char,int>mp2; int y=j,sum2=0; for(int z=1;z<=i;z++){ if(!mp2.count(s[y])){ sum2++; mp2[s[y]]=1; } y++; } if(sum2==i)return sum2; } } return 2; } };
|
https://leetcode.cn/problems/longest-substring-without-repeating-characters/