Beats 82% simple Java solution with explanation

  • 0
    I noticed that there is another similar solution but it goes here nonetheless. The idea is to sum up the points per character and then divide and see if the remainder is 0. For example, I am assuming the following:
    first row: 3 points
    2nd row: 5 points
    3rd row : 7 points.
    Input: ["Hello", "Alaska", "Dad", "Peace"]
    Output: ["Alaska", "Dad"]
    Now, "Dad" is actually 5*3/3*5=0. You can figure out the rest.
    public class Solution {
            String layer1= "qwertyuiop";//3 points each for first row.
            String layer2 = "asdfghjkl"; //5 points each for second row.
        public String[] findWords(String[] words) {
            int count=words.length;
            for(int i=0;i<words.length;i++) {
                int sum=0;
                Integer div=null;
                for(char c : words[i].toCharArray()) {
                    char lower = Character.toLowerCase(c);
                    sum += layer1.indexOf(lower)>=0?3:layer2.indexOf(lower)>=0?5:7;
                if(sum%(div*words[i].length())!=0) {
            String[] ret = new String[count];
            int idx=0;
            for(int i=0;i<words.length;i++) {
                if(words[i]!=null) {
                    ret[idx++] = words[i];
            return ret;

Log in to reply

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