[Leetcode] Remove Duplicate Letters Remove duplicate letters

Source: Internet
Author: User

Given a string which contains only lowercase letters, remove duplicate letters so this every letter appear once and only O nCE. You must make sure your result are 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.

This problem allows us to remove duplicate letters, so that each character can only appear once, and the results are sorted alphabetically, provided that the original relative position cannot be disturbed. Our way of thinking is: first set up a hash table to count the number of occurrences of each letter, but also need a visited number to record whether each letter has been visited, we traverse the entire string, for the traversed character, first in the hash table to reduce its value one, and then see if visited is accessed, If you have visited, continue the loop, stating that the letter has appeared in the results and that the position has been arranged properly. If not, we compare the last letter of the result, if the ASCII code of the letter is small and the result of the last letter in the hash table value is not 0 (indicating that the letter will appear later), then we have to delete the last letter in the result and mark it as not visited, It then adds the currently traversed letter and marks it as accessed, and so on until the entire string s is traversed, and the string in the result is the request. Here is a little trick, we start to give the result string res in the "0", is to be convenient for the first comparison, if it is empty can not be compared with the last character in Res, and ' 0 ' ASCII code is less than any one letter, so there is no problem. Finally we return the result and then remove the beginning of the ' 0 ', see the code as follows:

classSolution { Public:    stringRemoveduplicateletters (strings) {intm[ the] = {0}, visited[ the] = {0}; stringres ="0";  for(auto a:s) + +M[a];  for(Auto a:s) {--M[a]; if(Visited[a])Continue;  while(A < Res.back () &&M[res.back ()]) {Visited[res.back ()]=0;            Res.pop_back (); } Res+=A; Visited[a]=1; }        returnRES.SUBSTR (1); }};

You can use a recursive method if you are solving the problem in Java, see here. The idea is: first use a hash table to record the number of occurrences of each letter, and then traverse the given string s, find the smallest letter, each comparison of a letter, the value in the hash table minus 1, if this is 0, then do not continue to traverse, at this time we recorded a position, the string s in the position of the left character is deleted On the right of all the re-occurrence of the letter is also deleted, recursive call this function, in Java can use the ReplaceAll function, I use the STL myself to write a OJ can not pass the big data, may implement the method is wrong ...

Resources:

Https://leetcode.com/discuss/75529/c-simple-solution-easy-understanding

Https://leetcode.com/discuss/73761/a-short-o-n-recursive-greedy-solution


Leetcode all in one topic summary (continuous update ...)

[Leetcode] Remove Duplicate Letters Remove duplicate letters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.