C++ Why my output is NOT correct


  • 0
    B

    When input is "cbbbcaa".
    My code is right when tested on the VC, the output is "bca".
    But my result is reported as "ca". I can't figure out why.

    class Solution {
    public:
        string removeDuplicateLetters(string s) {
            string s_f;
    		string cc;
    		char strA[26]={0};
    		int iPos=-1,iValue=999;
    		int i,j;
    		int n;
    		int iV;
    		bool bTemp;
    		for(i=0;i<s.length();i++)
    		{
    			if(s[i]==s[i+1] && i != s.length()-1) continue;
    			strA[s[i]-'a']++;
    			if (s[i]-'a' < iValue)
    			{
    				iValue = s[i]-'a';
    				iPos = i;
    			}
    			s_f+=s[i];
    		}
    		cc="";
    		for(i=0;i<s_f.length();i++)
    		{
    			strA[s_f[i]-'a']--;
    			if(i==iPos) cc+=s_f[i];
    			if(s_f[i]==s_f[i+1] && i != s_f.length()-1) continue;
    			if(s_f[i]==s_f[iPos]) continue;
    			if(cc.find(s_f[i])!=-1) continue;
    			n = s_f.find(s_f[i],i+1);
    			if (n != i)
    			{
    				iV=s_f[i]-'a';
    				bTemp=false;
    				for(j=i+1;j<n;j++)
    				{
    					if( cc.find(s_f[j]) != -1) continue;
    					if(iV > (s_f[j]-'a')) 
    					{
    						bTemp=true;
    						break;
    					}
    					else
    					{
    						if(strA[s_f[j]-'a'] == 1)
    						{
    							bTemp=false;
    							break;
    						}
    					}
    				}
    				if(bTemp==false)
    				{
    					cc+=s_f[i];
    				}
    			}
    			else
    			{
    				cc+=s_f[i];
    			}
    		}
            return cc;
        }
    };
    

Log in to reply
 

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