No HashMap Java version. Super easy to understand and very short.

  • 0

    Using a simple math formula to see if all the characters are in the same line or not. Sum the line number for each character of a word and divide it by the line number of the first char of the word multipllied by the length of the word. If this is equal to one then the word can be formed using only one line of the keyboard

    public class Solution {
        public String[] findWords(String[] words) {
            ArrayList<String> res = new ArrayList<>();
            for(String a : words){
            return res.toArray(new String[0]);
        private boolean sameLineCheck(String a){
            a = a.toLowerCase();
            float sum = 0;
            float len = 0;
            String one = "qwertyuiop";
            String two = "asdfghjkl";
            float div = one.indexOf(a.charAt(0))>=0?1:(two.indexOf(a.charAt(0))>=0?2:3);
            for(char b : a.toCharArray()){
                sum += one.indexOf(b)>=0?1:(two.indexOf(b)>=0?2:3);
            return (sum/(div*len) == 1);

Log in to reply

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