Java Solution with Regex


  • 0
    H


    class Solution {
    public String[] findWords(String[] words) {
    String one = "qwertyuiop";
    String two = "asdfghjkl";
    String three = "zxcvbnm";
    List<String> res = new ArrayList<String>();
    for(String s:words){
    if(s.toLowerCase().replaceAll("[^" + one + "]", "").length()==s.length() ||
    s.toLowerCase().replaceAll("[^" + two + "]", "").length()==s.length() ||
    s.toLowerCase().replaceAll("[^" + three + "]", "").length()==s.length()){
    res.add(s);
    }
    }
    return res.toArray(new String[res.size()]);
    }
    }


Log in to reply
 

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