Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.

In the American keyboard:


	the first row consists of the characters "qwertyuiop",
	the second row consists of the characters "asdfghjkl", and
	the third row consists of the characters "zxcvbnm".


&nbsp;
Example 1:

Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]


Example 2:

Input: words = ["omk"]
Output: []


Example 3:

Input: words = ["adsdf","sfd"]
Output: ["adsdf","sfd"]


&nbsp;
Constraints:


	1 &lt;= words.length &lt;= 20
	1 &lt;= words[i].length &lt;= 100
	words[i] consists of English letters (both lowercase and uppercase).&nbsp;


class Solution:
    def findWords(self, words: List[str]) -> List[str]:
        first_row = "qwertyuiopQWERTYUIOP"
        second_row = "asdfghjklASDFGHIJKL"
        third_row = "zxcvbnmZXCVBNM"
        first_list = []
        second_list = []
        third_list = []
        one_line_words = []

        for word in words:
            if set(word).issubset(first_row):
                one_line_words.append(word)
            if set(word).issubset(second_row):
                one_line_words.append(word)
            if set(word).issubset(third_row):
                one_line_words.append(word)

        return one_line_words
class Solution {
    public String[] findWords(String[] words) {
        String row1 = "qwertyuiop";
        String row2 = "asdfghjkl";
        String row3 = "zxcvbnm";
        String res = "";
        for(int i=0; i<words.length; i++) {
            int sum1 = 0;
            int sum2 = 0;
            int sum3 = 0;
            for(int j=0; j<words[i].length(); j++) {
                if(row1.contains("" + words[i].toLowerCase().charAt(j))) {
                    sum1++;
                }
                else if(row2.contains("" + words[i].toLowerCase().charAt(j))) {
                    sum2++;
                }
                else if(row3.contains("" + words[i].toLowerCase().charAt(j))) {
                    sum3++;
                }
            }
            if(words[i].length()==sum1) {
                res += i;
            }
            else if(words[i].length()==sum2) {
            	res += i;
            }
            else if(words[i].length()==sum3) {
            	res += i;
            }
        }
		String[] resArr = new String[res.length()];
		for(int i=0; i<resArr.length; i++) {
			resArr[i] = words[Integer.parseInt("" + res.charAt(i))];
			System.out.println(resArr[i]);
		}
        return resArr;
    }
}
class DSU {
    vector<int> parent, size;

    vector<int> Q{'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
    vector<int> A{'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
    vector<int> Z{'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};

public:
    DSU(){
        for(int i= 0;i<123;i++) parent.push_back(i),size.push_back(1);
        for(int i = 0; i<Q.size()-1;i++) findUnion(Q[i],Q[i+1]);
        for(int i = 0; i<A.size()-1;i++) findUnion(A[i],A[i+1]);
        for(int i = 0; i<Z.size()-1;i++) findUnion(Z[i],Z[i+1]);
    }

    int findParent(int x){
        if(x==parent[x]) return x;
        return parent[x] = findParent(parent[x]);
    }

    void findUnion(int a, int b){
        a = findParent(a);
        b = findParent(b);
        if(a!=b) if(size[b]>size[a]) swap(a,b);parent[b] = a,size[a]+=size[b];
    }
};

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        int n = words.size();
        DSU dsu;
        vector<string> ans;
        for(int i = 0;i<n;i++){
            int ss = words[i].size();
            string s = words[i];
            if(ss==1) {ans.push_back(s);continue;}

            bool sameSet = true;
            for(int j = 0;j<ss-1;j++)
                if(dsu.findParent(s[j])!=dsu.findParent(s[j+1])){
                    sameSet = false;break;
                }
            if(sameSet)ans.push_back(s);
        }return ans;
    }
};
var findWords = function(words) {
const firstRow = {"q":true,"w":true,"e":true,"r":true,"t":true,"y":true,"u":true,"i":true,"o":true,"p":true }
const secondRow = {"a":true,"s":true,"d":true,"f":true,"g":true,"h":true,"j":true,"k":true,"l":true }
const thirdRow = {"z":true,"x":true,"c":true,"v":true,"b":true,"n":true,"m":true }
let result = [];

const helperFunc= (word)=>{
    let targetRow;
        //Determine in which row the word should be;
        if(firstRow[word[0].toLowerCase()]) targetRow = firstRow;
        if(secondRow[word[0].toLowerCase()]) targetRow = secondRow;
        if(thirdRow[word[0].toLowerCase()]) targetRow = thirdRow;
    
    for(let i = 1;i<word.length;i++) {
        if(!targetRow[word[i].toLowerCase()]) {
            return false;
        }
    }
    return true;
    }

    for(let i =0;i<words.length;i++) {
        if(helperFunc(words[i])) {
            result.push(words[i])
        }
    }
    return result;
};
Keyboard Row