示例 1:
示例 2:
package Stack;
public class p316_RemoveDuplicateLetters {
public static void main(String[] args) {
String s = "bcabc";
String res = removeDuplicateLetters(s);
System.out.println("res = " + res);
}
public static String removeDuplicateLetters(String s) {
StringBuffer res = new StringBuffer();
boolean[] visited = new boolean[26];
int[] hash = new int[26];
for (int i = 0; i < s.length(); i++) {
hash[s.charAt(i) - 'a']++;
}
for (int i = 0; i < s.length(); i++) {
if (!visited[s.charAt(i) - 'a']) {
while ((res.length() > 0) && (res.charAt(res.length() - 1) > s.charAt(i)) && (hash[res.charAt(res.length() - 1) - 'a']) >= 1) {
visited[res.charAt(res.length() - 1) - 'a'] = false;
res.deleteCharAt(res.length() - 1);
}
visited[s.charAt(i) - 'a'] = true;
res.append(s.charAt(i));
}
hash[s.charAt(i) - 'a']--;
}
return res.toString();
}
}
#include
char * removeDuplicateLetters(char * s)
{
int len = strlen(s);
char* res = (char *)malloc(sizeof(char) * 27);
int index = 0;
int hash[26] = { 0 };
int visited[26] = { 0 };
for (int i = 0; i < len; i++)
{
hash[s[i] - 'a']++;
}
for (int i = 0; i < len; i++)
{
if (!visited[s[i] - 'a'])
{
while ((index > 0) && (res[index - 1] > s[i]) && (hash[res[index - 1] - 'a'] >= 1))
{
visited[res[--index] - 'a'] = 0;
}
visited[s[i] - 'a'] = 1;
res[index++] = s[i];
}
hash[s[i] - 'a']--;
}
res[index] = '\0';
return res;
}
/*主函数省略*/