Easy Understand Java Solution


  • 0
    B
    public class Solution {
        public String[] findWords(String[] words) {
            String row1 = "[QWERTYUIOPqwertyuiop]+"; // 3 regexes to match words can be typed using these letters
            String row2 = "[ASDFGHJKLasdfghjkl]+";
            String row3 = "[ZXCVBNMzxcvbnm]+";
            ArrayList<String> res = new ArrayList<>();// result ArrayList
            for(String w : words){// for every word in words array 
                if(w.matches(row1) || w.matches(row2) || w.matches(row3)){
                    res.add(w);
                }
            }
            return res.toArray(new String[0]);// conver ArrayList to Array
        }
    }
    

Log in to reply
 

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