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"
Return "abc"

Given "cbacdcbc"
Return "acdb"

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.


根据字符串大小排序:优先把最小的字符放在前面,这样生成的字符肯定最小。问题是如何找出可以放在第一的最小字符呢?
     首先,找出s中出现的所有字符最后一次出现的位置,再从中找到最小的位置index1,从0到index1中找到最小字符,把选出来的字符做好标记
         我们可以选从0到index中的任意一个字符。其他所有待选字符都还在index-end中,等待之后选。
     然后在剩余待选字符中找到最后一次出现位置的最小值index2,再从index1+1 到index2里找出最小字符,把选出来的字符做好标记
     重复…

时间复杂度O(n)

public class Solution {
    public String removeDuplicateLetters(String s) {
        int[] index = new int[26];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            index[s.charAt(i) - 'a'] = i + 1;
        }
        int start = 0;
        int cur = findMinIndex(index); // Return the index of the characters' last occurance in the string
        while (cur >= 0) {
            int chosen = findMinChar(s, start, cur, index); // Find the index of the smallest char from 0 to 'cur' in the string
            start = chosen + 1;
            index[s.charAt(chosen) - 'a'] = 0;
            sb.append(s.charAt(chosen));
            cur = findMinIndex(index);
        }
        return sb.toString();
        
    }
    private int findMinIndex(int[] index) {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 26; i++) {
            if (index[i] > 0 && min > index[i]) {
                min = index[i];
            }
        }
        if (min == Integer.MAX_VALUE) {
            return -1;
        } else {
            return min - 1;
        }
    }
    private int findMinChar(String s, int start, int end, int[] index) {
        int min = -1;
        char c = '{';
        for (int i = start; i <= end; i++) {
            if (index[s.charAt(i) - 'a'] > 0 && c > s.charAt(i)) {
                c = s.charAt(i);
                min = i;
            }
        }
        return min;
    }
}

 

转载于:https://www.cnblogs.com/yuchenkit/p/7169500.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值