Advertisement
Guest User

longest unique subsequence

a guest
Nov 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. private static int longestUniqueSubstring(String s){
  2.         Map<Character, Integer> visitedCharsAndTheirIndices = new HashMap<>();
  3.         int maxLen = 0;
  4.         int currLen = 0;
  5.  
  6.  
  7.         for(int i=1; i<s.length(); i++){
  8.             char currElem = s.charAt(i);
  9.             if(!visitedCharsAndTheirIndices.containsKey(currElem) || visitedCharsAndTheirIndices.get(currElem) < i - currLen){
  10.                 currLen++;
  11.             } else{
  12.                 maxLen = currLen > maxLen ? currLen : maxLen;
  13.                 currLen = i - visitedCharsAndTheirIndices.get(currElem);
  14.             }
  15.  
  16.             visitedCharsAndTheirIndices.put(currElem, i);
  17.  
  18.         }
  19.  
  20.         maxLen = currLen > maxLen ? currLen : maxLen;
  21.         return maxLen;
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement