SHARE
TWEET

longest unique subsequence

a guest Nov 19th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top