(DP)3.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.

public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0)
return 0;
String[] dp = new String[s.length()];
dp[0] = s.substring(0, 1);
int index = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1))
dp[i] = s.substring(i - 1, i);
else {
index = dp[i - 1].indexOf(s.charAt(i));
if (index == -1)
dp[i] = dp[i - 1] + s.charAt(i);
else
dp[i] = dp[i - 1].substring(index+1) +s.charAt(i);
} }
index = 0;
for (int i = 0; i < s.length(); i++) {
if (dp[i].length() > index)
index = dp[i].length();
}
return index;
}
}

  

上一篇:eclipse安装hibernate


下一篇:linux实用指令---持续更新