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 result is the smallest in lexicographical order among all possible results.

Example:

Given "bcabc"
Return "abc"

Given "cbacdcbc"
Return "acdb"

class Solution {
public:
    string removeDuplicateLetters(string s) {
        int size = s.size();

        vector<int>  vec_cnts(26,0);

        for(int i=0; i<size ;++i){
            vec_cnts[s[i]-‘a‘]++;
        }

        string res ;

        int res_ch_pos = -1;

        for(int i=0; i<size; ++i){
            if(vec_cnts[s[i]-‘a‘] == 0){
                continue;
            } 

            if(vec_cnts[s[i]-‘a‘] != 1) {
                vec_cnts[s[i]-‘a‘]--;
                continue;
            }

            char ch     = s[i] ;
            int  ch_pos = i;
            int  j      = i-1;

            while(j  > res_ch_pos ){//从后往前找到第一小于ch的字符,若是没找到小于的字符,则找第一个等于ch的字符
                if(s[j] > ch  || vec_cnts[s[j]-‘a‘] == 0) {
                    --j;
                    continue;
                }
                if(s[j] < ch){//小于
                    ch     = s[j];
                    ch_pos = j;
                }else{//相等
                    ch_pos = j;
                }
                --j;
            }

            res.push_back(s[ch_pos]);//把找到的字符加入结果集

            vec_cnts[s[ch_pos]-‘a‘] = 0;//该字符已经使用,下次不能再使用了

            res_ch_pos = ch_pos;//更正当前结果中,最后一个字符所在的位置

            if(res_ch_pos < i){//如果结果字符在i之前,下次仍从i开始往前找
                i -= 1;
            }
        }

        return res;
    }
};
/*
"bbbacacca"

*/

Next challenges: (H) Jump Game II (H) Candy (M) Meeting Rooms II

时间: 2024-10-18 18:47:00

316. Remove Duplicate Letters的相关文章

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

[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] 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 "bca

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

[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

[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

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] 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

How to remove duplicate lines in a large text file?

How would you remove duplicate lines from a file that is  much too large to fit in memory? The duplicate lines are not necessarily adjacent, and say the file is 10 times bigger than RAM. A better solution is to use HashSet to store each line of input