Simple C++ solution, easy to understand


  • 0
    S
    vector<string> findWords(vector<string>& words) {
    vector<string> find;
    string s1 = { "QqWwEeRrTtYyUuIiOoPp" };
    string s2 = { "AaSsDdFfGgHhJjKkLl" };
    string s3 = { "ZzXxCcVvBbNnMm" };
    for (auto vec : words)	//search in every word
    {
    	int flag1 = 1;
    	int flag2 = 1;
    	int flag3 = 1;
    	auto iter1 = vec.begin();
    	auto iter2 = vec.begin();
    	auto iter3 = vec.begin();
    	while (flag1 != 0 && iter1 != vec.end())
    	{
    		if (s1.find(*iter1) == string::npos)
    		{
    			flag1 = 0;
    		}
    		iter1++;
    	}
    	if (flag1 == 1) 
    	{
    		find.push_back(vec);
    		flag2 = 0;
    	}		
    	while (flag2 != 0 && iter2 != vec.end())
    	{
    		if (s2.find(*iter2) == string::npos)
    		{
    			flag2 = 0;
    		}
    		iter2++;
    	}
    	if (flag2 == 1)
    	{
    		find.push_back(vec);
    		flag3 = 0;
    	}		
    	while (iter3 != vec.end())
    	{
    		if (flag3 != 0 && s3.find(*iter3) == string::npos)
    		{
    			flag3 = 0;
    		}
    		iter3++;
    	}
    	if (flag3 == 1)
    	{
    		find.push_back(vec);
    	}
    }
    return find;
        
    }

Log in to reply
 

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