[Leetcode] [JavaScript] Remove Duplicate Letters

Source: Internet
Author: User

Remove Duplicate Letters

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"

https://leetcode.com/problems/maximum-product-of-word-lengths/

Test instructions is to remove the duplicate letters, leaving the letters with the smallest alphabetic order.

The first solution, greed, complexity O (n^2).

Each round counts the number of occurrences of letters, from left to right to find the smallest or only one occurrence of the letter, if the smallest letter has multiple, take the leftmost one.

1 /**2 * @param {string} s3 * @return {string}4  */5 varRemoveDuplicateLetters2 =function(s) {6     varres = "", I, pos, Countch = {}, Charcodea = ' a '. charCodeAt (0);7      while(S!== ""){8          for(i = 0; i < i++) Countch[string.fromcharcode (i + charcodea)] = 0;9          for(i = 0; i < s.length; i++) countch[s[i]]++;Tenpos = 0; One          for(i = 0; i < s.length; i++){ A             if(S[i] < S[pos]) pos =i; -             if(Countch[s[i]] = = = 1) Break; -countch[s[i]]--; the         } -Res + =S[pos]; -s = s.substring (pos + 1, s.length). Replace (NewREGEXP (S[pos], ' g '), '); -     } +     returnRes;  -};

The second method uses a stack, the complexity of the complex O (n).

The end of the maintenance stack is the result, and the number of occurrences of all letters should be counted at the beginning.

Putting letters into an array is a greedy process, each cycle, if the letter is not in the stack, the current element is in the stack.

But before checking the letters in the stack, if the top letter of the stack is larger than the current letter and the top of the stack will appear, then the stack, because it is placed in the back of the letter order is relatively small, such as the second B in the Bab.

1 /**2 * @param {string}3 * @return {string}4  */5 varRemoveduplicateletters =function(s) {6     varI, Countch = {}, Charcodea = ' a '. charCodeAt (0), stack = [], visited ={}, top;7      for(i = 0; i < i++) Countch[string.fromcharcode (i + charcodea)] = 0;8      for(i = 0; i < s.length; i++) countch[s[i]]++;9      for(i = 0; i < s.length; i++){Tencountch[s[i]]--; One         if(Visited[s[i]])Continue; Atop = Stack[stack.length-1]; -          for(; stack.length > 0 && countch[top] > 0 && s[i] < top; top = Stack[stack.length-1]){ -Visited[top] =false; the Stack.pop (); -         } - Stack.push (S[i]); -Visited[s[i]] =true; +     } -     returnStack.join (' ');  +};

[Leetcode] [JavaScript] 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.