lundi 22 mars 2021

If statement provides false for only one string value

Hey guys i have been trying to leet code to improve my coding skills. I came across an "easy" question on it and was trying out my own solution. while doing this I found out something surprising.

my if statement provides a false value for the "c" string but works on all of the others, I have tried checking with other string values and they work. any reason why my code is providing a false result for this statement?

here is my code

    string allowed ="abc";
    vector<string> words = 
    {
    "a",
    "b",
    "c",
    "ab",
    "ac",
    "bc",
    "abc"
    };
    
    int count = 0;
    bool contains;
    
    for(string w: words)
    {
        for(auto c: w)
        {
            if(allowed.find(c) <= w.size())
            {
              contains = true;  
            }else
            {
                contains = false;
                break;
            }
                
        }
        
        if(contains)
        {
            count++;   
        }
    }
    
    return count;

my question is are there any reason why my code is providing a false result for the "c" string? or is there something wrong with my if else statement.

how can I fix this?

thank you so much in advance to everyone who helped me out!

Aucun commentaire:

Enregistrer un commentaire