JAVA---------Easy Version To UnderStand!!!!!!!!!!!!


  • -1
    H
    	public static String removeDuplicateLetters(String s) {
    	if (s == null || s.length() == 0)
    		return "";
    	int len = s.length();
    	HashMap<Character, Integer> map = new HashMap<>();
    	for (int i = 0; i < len; i++) {
    		if (map.get(s.charAt(i)) == null)
    			map.put(s.charAt(i), 1);
    		else
    			map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
    	}
    	int pos = 0;
    	for (int i = 0; i < len; i++) {
    		if (s.charAt(i) < s.charAt(pos))
    			pos = i;
    		map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
    		if (map.get(s.charAt(i)) == 0) {
    			return s.charAt(pos) + removeDuplicateLetters(s.substring(pos + 1).replaceAll(s.charAt(pos) + "", ""));
    		}
    
    	}
    	return "";
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.