[leetcode] Remove Duplicate Letters

题目:

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

Example:
Given "bcabc"
Return "abc"

Given "cbacdcbc"
Return "acdb"

分析:通过观察每个字母下标的规律,以"cbacdcbc"为例,

第一步,计算下标(countIndex):

a  => 2,  

b  => 1, 6

c  => 0, 3, 5, 7

d  => 4

第二步,寻找符合条件的字母(findLetter):

如果一个字母的第一个下标小于其后每个字母的最后一个下标,则该字母符合条件。比如 a 中的 2 小于 b 的 6,c 的 7,d 的 4,则 a 中的2符合条件。

第三步,删除前面的下标(removeIndex):

不防设第二步中符合条件的字母为X,则删除X下标之前的所有下标,并删除X。比如X为a,X下标为2,则删除 b 中的 1,c 中的 0,并把 a 删除。

重复第二步,第三步,直到集合为空。

以 "cbacdcbc" 为例:

b  => 6

c  => 3, 5, 7

d  => 4
b  => 6

d  => 4

上述变换解释:虽然 6 < 7,但 6 却 > 4, 故 b 不符合,而 c 中的 3 小于其后的 4,故 c 符合。

b  => 6

故最后生成 a -> c -> d -> b,即 acdb.

代码如下:

    public String removeDuplicateLetters(String s) {
        StringBuilder res = new StringBuilder();
        HashMap<Character, ArrayList<Integer>> hm = new HashMap<Character, ArrayList<Integer>>();
        ArrayList<Character> reference = new ArrayList<Character>();
        countIndex(s, hm, reference);
        Collections.sort(reference);
        while (!reference.isEmpty()) {
            int lettIndex = findLetter(s, res, hm, reference);
            removeIndex(lettIndex, hm, reference);
        }
        return res.toString();
    }

    private void countIndex(String s, HashMap<Character, ArrayList<Integer>> hm, ArrayList<Character> reference) {
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (! hm.containsKey(ch)) {
                ArrayList<Integer> tmp = new ArrayList<Integer>();
                tmp.add(i);
                hm.put(ch, tmp);
                reference.add(ch);
            } else {
                hm.get(ch).add(i);
            }
        }
    }

    private int findLetter(String s, StringBuilder res, HashMap<Character, ArrayList<Integer>> hm, ArrayList<Character> reference) {
        int m = 0;
        for (int i = 0; i < reference.size(); i++) {
            m = hm.get(reference.get(i)).get(0);
            int j = i+1;
            for (; j < reference.size(); j++) {
                ArrayList<Integer> tmp = hm.get(reference.get(j));
                if (m > tmp.get(tmp.size()-1)) {
                    break;
                }
            }
            if (j == reference.size()) {
                res.append(reference.get(i));
                reference.remove(i);
                break;
            }
        }
        return m;
    }

    private void removeIndex(int lett, HashMap<Character, ArrayList<Integer>> hm, ArrayList<Character> reference) {
        for (int i = 0; i < reference.size(); i++) {
            ArrayList<Integer> tmp = hm.get(reference.get(i));
            while (!tmp.isEmpty() && tmp.get(0) < lett) {
                tmp.remove(0);
            }
        }
    }
时间: 2024-10-27 01:25:57

[leetcode] Remove Duplicate Letters的相关文章

[LeetCode] Remove Duplicate Letters 移除重复字母

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q

[LeetCode][JavaScript]Remove Duplicate Letters

Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results

leetcode 316. Remove Duplicate Letters

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q

316. Remove Duplicate Letters

316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your resu

Remove Duplicate Letters I &amp; II

Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. Example: Given "bcabc"Return "abc" Given "cbacdcbc"Return "abcd&qu

[Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcab

[Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3. 这题和R

Remove Duplicate Letters

1 public class Solution { 2 public String removeDuplicateLetters(String s) { 3 if (s.length() < 2) { 4 return s; 5 } 6 int[] letters = new int[26]; 7 for (char c : s.toCharArray()) { 8 letters[c - 'a']++; 9 } 10 boolean[] visited = new boolean[26]; 1

LeetCode #316 Remove-Duplicate Letters 数组 字符串

Description Given a string which contains only lowercase letters, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: