头图

LeetCode-500-Keyboard OK

雄狮虎豹
中文

Keyboard row

Title description: Give you a string array words , only return words that can be printed using the letters on the same line of the American keyboard

Please refer to the official website of LeetCode for examples.

Source: LeetCode
Link: https://leetcode-cn.com/problems/keyboard-row/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Traverse characters

First, if words is null or words is an empty array, then the empty array is returned directly, that is, there is no word that meets the conditions.

Otherwise, first initialize a the Map to characterMap for all of the characters and the number of lines to save the corresponding row, and then traversed words word of Word :

  • First get the first character in word firstCharacter , according to characterMap judge the first character firstCharacter
  • Then, judge whether word are all in the rowNum row, if not, skip processing the next word; if so, add the word to the result set.

Finally, the words in the result set are returned.

import java.util.*;

/**
 * @Author: ck
 * @Date: 2021/10/3 10:47 上午
 */
public class LeetCode_500 {
    private static final Map<Integer, Set<Character>> characterMap = new HashMap<>();

    static {
        characterMap.put(1, new HashSet<>(Arrays.asList(new Character[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'})));
        characterMap.put(2, new HashSet<>(Arrays.asList(new Character[]{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'})));
        characterMap.put(3, new HashSet<>(Arrays.asList(new Character[]{'z', 'x', 'c', 'v', 'b', 'n', 'm'})));
    }

    public static String[] findWords(String[] words) {
        if (words == null || words.length == 0) {
            return new String[0];
        }
        String[] result = new String[words.length];
        int index = 0, size = 0;
        for (String word : words) {
            if (word == null || word.length() == 0) {
                result[index++] = word;
                size++;
                continue;
            }
            char[] wordArr = word.toCharArray();
            char firstCharacter = wordArr[0];
            int rowNum = 1;
            for (Map.Entry<Integer, Set<Character>> characterEntry : characterMap.entrySet()) {
                if (characterEntry.getValue().contains(Character.toLowerCase(firstCharacter))) {
                    rowNum = characterEntry.getKey();
                    break;
                }
            }
            int i;
            for (i = 1; i < word.length(); i++) {
                if (!characterMap.get(rowNum).contains(Character.toLowerCase(wordArr[i]))) {
                    break;
                }
            }
            if (i == word.length()) {
                result[index++] = word;
                size++;
            }
        }
        return Arrays.copyOf(result, size);
    }

    public static void main(String[] args) {
        String[] words = new String[]{"Hello", "Alaska", "Dad", "Peace"};
        for (String word : findWords(words)) {
            System.out.println(word);
        }
    }
}
【Daily Message】 Look at the world plainly, and write life down-to-earth.
阅读 505

LeetCodet题解
技术学习

玉树临风,仙姿佚貌!

1.2k 声望
6.1k 粉丝
0 条评论

玉树临风,仙姿佚貌!

1.2k 声望
6.1k 粉丝
文章目录
宣传栏