Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static int longestUniqueSubstring(String s){
- Map<Character, Integer> visitedCharsAndTheirIndices = new HashMap<>();
- int maxLen = 0;
- int currLen = 0;
- for(int i=1; i<s.length(); i++){
- char currElem = s.charAt(i);
- if(!visitedCharsAndTheirIndices.containsKey(currElem) || visitedCharsAndTheirIndices.get(currElem) < i - currLen){
- currLen++;
- } else{
- maxLen = currLen > maxLen ? currLen : maxLen;
- currLen = i - visitedCharsAndTheirIndices.get(currElem);
- }
- visitedCharsAndTheirIndices.put(currElem, i);
- }
- maxLen = currLen > maxLen ? currLen : maxLen;
- return maxLen;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement