Find words that can be typed using only one row on a keyboard on leetcode.com, there is a question with the following description: 


  Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
  link to the question here.


Here is my implementation: 

class Solution {
public:
    std::vector&lt;std::string&gt; findWords(std::vector&lt;std::string&gt;&amp; words) 
    {
        std::unordered_set&lt;char&gt; firstRow = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'};
        std::unordered_set&lt;char&gt; secondRow = {'A','S','D','F','G','H','J','K','L'};
        std::unordered_set&lt;char&gt; thirdRow = {'Z','X','C','V','B','N','M'};
        std::vector&lt;std::string&gt; result;

        size_t vecSize = words.size();
        for(size_t i = 0; i &lt; vecSize; ++i)
        {
                if(sameRow(words[i], firstRow))
                    result.push_back(words[i]);
                else if(sameRow(words[i], secondRow))
                    result.push_back(words[i]);
                else if(sameRow(words[i], thirdRow))
                    result.push_back(words[i]);
        }
        return result;
    }
    bool sameRow(const std::string&amp; words, const std::unordered_set&lt;char&gt;&amp; row)
    {
                size_t wordSize = words.size();
                for(size_t j = 0; j &lt; wordSize; ++j)
                {
                    if(row.find(toupper(words[j])) == row.end())
                    {
                       return false;
                    }
                }
        return true;
    }
};
Algorithm/Data Structure Selection

Right now you're using three separate sets, one of the letters in each row of the keyboard.

Instead, I think I'd use a single map from character to row number. Then you can start with the first character in the string, and find what row it's on. Then you check whether the other characters in the word are in the same row. Right now, you may look up each character in a string up to three separate times. Changing the algorithm, you can reduce that to (at most) one lookup per character.

Since you have a dense set of inputs for the map (i.e., only ASCII letters) I'd probably also use an array/vector for the mapping, instead of an unordered map. A vector/array will avoid hashing, and it's a dense structure, so you get really good cache utilization.

Avoid Unnecessary Transformations

It's probably simpler to create a mapping for all possible input characters instead of using toupper on the input, then looking it up in the set/map.

Consider other Looping Constructs

Instead of a loop like this:

            size_t wordSize = words.size();
            for(size_t j = 0; j &lt; wordSize; ++j)
            {
                if(row.find(toupper(words[j])) == row.end())
                {
                   return false;
                }
            }


I'd probably use a loop more like this:

for (auto c : words[j] )
    if (row.find(c) == row.end())
        return false;
return true;


...or you could use a standard algorithm instead:

return std::all_of(words[j].begin(), words[j].end(), 
    [&amp;](char c) { return row.find(c) != row.end(); });


(Though, as outlined above, I'd rather change it to look up the row number instead).