A challenge string
A longest non-repeating sub-character string
Given a string, you find out which does not contain a sub-length of the longest string of repeated characters.
analysis:
When the hash table index record characters recent, i traversed position, seeking the current character position i is the longest non-repeating sub-string end.
code:
class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) { return 0; } Map<Character, Integer> map = new HashMap<Character, Integer>(); int start = -1; int res = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (!map.containsKey(c)) { map.put(c, -1); } start = Math.max(start, map.get(c)); map.put(c, i); res = Math.max(res, i - start); } return res; } }