Given a string, find the length of the smallest string that does not contain repeating characters

(1) If you want to check all strings, it is not recommended. (2) If you want to check all strings, it is not recommended. Sliding window method, with two Pointers to define a window, the window kept moving towards the end of the string, and a hashMap record has been window contains characters, when the window when they arrived at the end it is concluded that the length of the longest string, (if you need to know that a string is, at the time of calculation need to keep the window subscript)

public int getLongestSubString(String s) { int len = s.length(); int res = 0; // Key is a Character and value is a subscript Map<Character, Integer> Map = new HashMap(); For (int I = 0, j = 0; j < len; J ++) {if (map.containsKey(s.charat (j))) {I = math.max (map.get(s.charat (j)), I); } res = Math.max(res, j - i + 1); map.put(s.charAt(j), j + 1); } return res; }Copy the code

The next issue is the longest palindrome string or the median of two ordered arrays left from the previous issue

Longest palindrome string:

    public String getLongestPalindrome(String s) {
	if (s == null || s.length() < 1) {
		return s;
	}

	int maxStart = 0;
	int maxEnd = 0;
	for (int i = 0; i < s.length(); i ++) {
		int len1 = getLongestByCenter(s, i ,i);
		int len2 = getLongestByCenter(s, i, i + 1);
		int len = Math.max(len1, len2);
		if (len > maxEnd - maxEnd) {
			maxEnd = i + len/2;
			maxStart = i - (len - 1)/2;
		}
	}
	return s.substring(maxStart, maxEnd + 1);


}

public int getLongestByCenter(String s, int left, int right) {
	while (left > 0 && right < s.length()
			&& s.charAt(left) == s.charAt(right)) {
		left --;
		right ++;
	}
	return right - left - 1;
}Copy the code