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;i<s.size();i++)
        {
            for(int j=0;j<s.size();j++)
            {
                if(i==j)
                    dp[i][j]=1;
            }
        }
        int MaxLen=1;
        for(int i=0;i<s.size();i++)
        {
            for(int j=i+1;j<s.size();j++)
            {
                string tmp=s.substr(i,j-i);
                if(dp[i][j-1] && (tmp.find(s[j])==string::npos))
                {
                    dp[i][j]=1;
                    if(MaxLen<j-i+1)
                    {
                        MaxLen=j-i+1;
                    }
                }
                else
                    break;
            }
        }
        return MaxLen;
    }
};
发布了65 篇原创文章 · 获赞 12 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览