Keyboard Row

原文地址:https://leetcode.com/problems/keyboard-row/description/

Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.

这里写图片描述

Example 1:

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

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

代码实现:

public class KeyboardRow {
    private static final HashSet<Character> firstRowInKeyboard = new HashSet<Character>() {
        {
            this.add('Q');
            this.add('q');
            this.add('W');
            this.add('w');
            this.add('E');
            this.add('e');
            this.add('R');
            this.add('r');
            this.add('T');
            this.add('t');
            this.add('Y');
            this.add('y');
            this.add('U');
            this.add('u');
            this.add('I');
            this.add('i');
            this.add('O');
            this.add('o');
            this.add('P');
            this.add('p');
        }
    };

    private static final HashSet<Character> secondRowInKeyboard = new HashSet<Character>() {
        {
            this.add('A');
            this.add('a');
            this.add('S');
            this.add('s');
            this.add('D');
            this.add('d');
            this.add('F');
            this.add('f');
            this.add('G');
            this.add('g');
            this.add('H');
            this.add('h');
            this.add('J');
            this.add('j');
            this.add('K');
            this.add('k');
            this.add('L');
            this.add('l');
        }
    };

    private static final HashSet<Character> thirdRowInKeyboard = new HashSet<Character>() {
        {
            this.add('Z');
            this.add('z');
            this.add('X');
            this.add('x');
            this.add('C');
            this.add('c');
            this.add('V');
            this.add('v');
            this.add('B');
            this.add('b');
            this.add('N');
            this.add('n');
            this.add('M');
            this.add('m');
        }
    };

    public static String[] findWords(String[] words) {
        if (words == null)
            return null;

        if (words.length == 0)
            return new String[]{};

        List<String> resultList = new ArrayList<>();

        for (int i = 0; i < words.length; i++) {
            if (isInSameRow(words[i]))
                resultList.add(words[i]);
            else
                continue;
        }

        String[] resultArray = new String[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            resultArray[i] =resultList.get(i);
        }

        return resultArray;
    }

    public static boolean isInSameRow(String word) {
        if (word == null || word.length() == 0)
            return true;

        char firstChar = word.charAt(0);
        HashSet<Character> set = null;
        if (firstRowInKeyboard.contains(firstChar))
            set = firstRowInKeyboard;
        else if (secondRowInKeyboard.contains(firstChar))
            set = secondRowInKeyboard;
        else if (thirdRowInKeyboard.contains(firstChar))
            set = thirdRowInKeyboard;
        else
            return false;

        for (int i = 0; i < word.length(); i++) {
            if (set.contains(word.charAt(i)))
                continue;
            else
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        String[] strArray = new String[] {"Hello", "Alaska", "Dad", "Peace"};
        System.out.println(Arrays.toString(findWords(strArray)));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值