zoukankan      html  css  js  c++  java
  • [LeetCode 1081] Smallest Subsequence of Distinct Characters

    Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once.

    Example 1:

    Input: "cdadabcc"
    Output: "adbc"
    

    Example 2:

    Input: "abcd"
    Output: "abcd"
    

    Example 3:

    Input: "ecbacba"
    Output: "eacb"
    

    Example 4:

    Input: "leetcode"
    Output: "letcod"
    

     

    Constraints:

    1. 1 <= text.length <= 1000
    2. text consists of lowercase English letters.

    Key observation: if the current character has not been selected before and is smaller than the previous character(s), then we should try to remove these bigger characters. A special case of this is that a previous character is bigger than the current character but it is the last one of its instances. We can not remove this character because we need each unique character exactly once.

    Based on this, we have the following algorithm:

    1. get character frequency count mapping.

    2. go over input string s, for each character, decrease its count by 1. If the current character c has been picked, go to the next character in s. 

    3. otherwise, remove all bigger characters that can still be added later from the top of the stack. For each popped character, update its picked boolean to false.

    4. add c to the stack and mark its picked boolean to true.

    5. after going over s, return all the remaining characters as the final result.

    class Solution {
        public String removeDuplicateLetters(String s) {
            int[] cnt = new int[26];
            boolean[] picked = new boolean[26];
            for(int i = 0; i < s.length(); i++) {
                cnt[s.charAt(i) - 'a']++;
            }
            ArrayDeque<Character> dq = new ArrayDeque<>();
            for(int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                int diff = c - 'a';
                cnt[diff]--;
                if(picked[diff]) {
                    continue;
                }
                while(dq.size() > 0 && dq.peekLast() > c && cnt[dq.peekLast() - 'a'] > 0) {
                    char rm = dq.pollLast();
                    picked[rm - 'a'] = false;
                }
                dq.addLast(c);            
                picked[diff] = true;
            }
            StringBuilder buffer = new StringBuilder();
            while(dq.size() > 0) {
                buffer.append(dq.pollFirst());
            }
            return buffer.toString();
        }
    }
  • 相关阅读:
    MVC3中输出Html标签的方法
    Server.MapPath 出现未将对象引用设置到对象的实例
    谈谈网站静态化
    WCF 服务应用程序与 服务库之间的区别
    插入中国所有省和市的SQL语句--以后用
    KiCad 元件值 F4NNIU 规范 (2020-04-30)[31.98%]
    FastAdmin 安装后点登录没有反应怎么办?
    笔记:读英国老太太的复仇计划 (2019-10-15)
    KiCad 工程用 Git 管理需要忽略哪些文件?
    关于 SSD 的接口和相关名词(2019-09-10)
  • 原文地址:https://www.cnblogs.com/lz87/p/12971903.html
Copyright © 2011-2022 走看看