zoukankan      html  css  js  c++  java
  • 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 1:

    Input: "bcabc"
    Output: "abc"
    

    Example 2:

    Input: "cbacdcbc"
    Output: "acdb"

    intuition: The leftmost letter in our solution will be the smallest letter such that the suffix from that letter contains every other. This is because we know that the solution must have one copy of every letter, and we know that the solution will have the lexicographically smallest leftmost character possible. If there are multiple smallest letters, then we pick the leftmost one simply because it gives us more options. We can always eliminate more letters later on, so the optimal solution will always remain in our search space.

    reference: https://leetcode.com/problems/remove-duplicate-letters/solution/

    M1:  Greedy - solving letter by letter

    We use idea number one from the intuition. In each iteration, we determine leftmost letter in our solution. This will be the smallest character such that its suffix contains at least one copy of every character in the string. We determine the rest our answer by recursively calling the function on the suffix we generate from the original string (leftmost letter is removed).

    time = O(n), space = O(n)

    class Solution {
        public String removeDuplicateLetters(String s) {
            int[] cnt = new int[26];
            for(char c : s.toCharArray()) {
                cnt[c - 'a']++;
            }
            
            int pos = 0;    // the index of the smallest character before iteration ends
            for(int i = 0; i < s.length(); i++) {
                if(s.charAt(i) < s.charAt(pos)) {
                    pos = i;
                }
                if(--cnt[s.charAt(i) - 'a'] == 0) {
                    break;
                }
            }
            // answer is the leftmost letter plus the recursive call on the rest of the string
            return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters(s.substring(pos + 1).replaceAll("" + s.charAt(pos), ""));
        }
    }

    M2: 

    class Solution {
        public String removeDuplicateLetters(String s) {
            StringBuilder sb = new StringBuilder();
            int[] cnt = new int[26];
            boolean[] seen = new boolean[26];
            
            char[] chs = s.toCharArray();
            for(char c : chs) {
                cnt[c - 'a']++;
            }
            
            for(char c : chs) {
                cnt[c - 'a']--;
                if(seen[c - 'a']) {
                    continue;
                }
                // when the last character in sb is larger than c and appears more than once, delete it from sb
                while(sb.length() > 0 && sb.charAt(sb.length() - 1) > c && cnt[sb.charAt(sb.length() - 1) - 'a'] > 0) {
                    seen[sb.charAt(sb.length() - 1) - 'a'] = false;
                    sb.deleteCharAt(sb.length() - 1);
                }
                sb.append(c);
                seen[c - 'a'] = true;
            }
            return sb.toString();
        }
    }
  • 相关阅读:
    C++ XML解析之TinyXML篇[转]
    TinyXML:一个优秀的C++ XML解析器[转]
    nginx 出现413 Request Entity Too Large问题的解决方法
    redis配置认证密码
    《Discuz安装时候出现乱码 -- 问题解决方法》
    MySQL创建用户与授权
    CentOS 7 安装mysql
    setfacl命令 来自: http://man.linuxde.net/setfacl
    install pip3 for python 3.x
    自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书
  • 原文地址:https://www.cnblogs.com/fatttcat/p/11399494.html
Copyright © 2011-2022 走看看