longest-substring-without-repeating-characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
         if(s.size()==0) return 0;
        if(s.size()==1) return 1;
        vector<vector<int>> dp(s.size(),vector<int>(s.size(),0));
        for(int i=0;ifor(int j=0;jif(i==j)
                    dp[i][j]=1;
            }
        }
        int MaxLen=1;
        for(int i=0;ifor(int j=i+1;jstring tmp=s.substr(i,j-i);
                if(dp[i][j-1] && (tmp.find(s[j])==string::npos))
                {
                    dp[i][j]=1;
                    if(MaxLen1)
                    {
                        MaxLen=j-i+1;
                    }
                }
                else
                    break;
            }
        }
        return MaxLen;
    }
};

你可能感兴趣的:(longest-substring-without-repeating-characters)