why i got "bacd" is wrong answer for "cbacdcbc" ?


  • 0
    K

    why answer for "cbacdcbc" is "acdb"

    0_1470279749096_Remove Duplicate Letters  LeetCode OJ.png
    but i got "bacd" is best , why?
    Inversion number of "acdb" is 0 + 1 + 1 + 0 =2 which is "cb" ,"db"
    Inversion number my answer "bacd" is 1 + 0 + 0 + 0 = 1 which is "ba"
    is the answer wrong?? i don't know why .
    here is my code ,not efficient though
    ‘’‘
    class Solution {
    public:
    string removeDuplicateLetters(string s) {
    string result(26,'\0');
    vector<int> chArr(26,-1);
    int StrLen = 0;
    char last;
    for(int i = 0;i<s.size();++i)
    {
    if(chArr[s[i] - 'a'] == -1){
    chArr[s[i] - 'a'] = StrLen;
    StrLen++;
    last = s[i];
    }else{
    int k=0;
    for(char temp = s[i] + 1;temp<='z';++temp)
    {
    if(chArr[temp - 'a'] > -1)
    k++;
    }
    int kPrev = 0;
    for(char temp = 'a';temp<='z';++temp)
    {
    if(chArr[temp - 'a'] > -1 && ((temp < s[i] && chArr[temp - 'a'] > chArr[s[i] - 'a']) ||
    (temp > s[i] && chArr[temp - 'a'] < chArr[s[i] - 'a'])))
    kPrev++;
    }
    if(k < kPrev || (k == kPrev && last < s[i])){
    for(int temp = 0;temp<26;++temp)
    {
    if(chArr[temp] > chArr[s[i] - 'a'])
    chArr[temp]--;
    }
    chArr[s[i] - 'a'] = StrLen - 1;
    }
    }
    }
    for(int i = 0;i<=26;++i)
    if(chArr[i] > -1)
    result[i] = 'a' + chArr[i];
    result.resize(StrLen);
    return string(result);
    }

    };
    ’‘’


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.