Concise/Clean C++ Solution


  • 1
    W
    vector<string> findWords(vector<string>& words) {
            vector<string> hash = {"QWERTYUIOP","ASDFGHJKL","ZXCVBNM"}, result;
            vector<int> mp(128, -1);
            for(int i = 0; i < hash.size(); i++) 
                for(char c: hash[i]) mp[c] = i;
            for(auto& str: words) {
                int i = 0;
                for(; i < str.size()-1; i++)
                    if(mp[toupper(str[i])] != mp[toupper(str[i+1])]) break;
                if(i == str.size()-1) result.push_back(str);
            }
            return result;
        }
    

  • 0
    X

    @wangxinbo i use the same method as you do, instead , i just list the map result by manual:

    int ch2line(char ch) {
    	int alphaLine[26] = { 2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, 3, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3 };
    	return (ch < 'a') ? alphaLine[ch - 'A'] : alphaLine[ch - 'a'];
    }

  • 0
    W

    @xikafe Neat code, bro! But I believe my code is easy to type in : )


Log in to reply
 

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