2021-02-28 面試題 16.20. T9鍵盤

題目地址

https://leetcode-cn.com/problems/t9-lcci/

題目描述

在老式手機(jī)上,用戶通過數(shù)字鍵盤輸入,手機(jī)將提供與這些數(shù)字相匹配的單詞列表。每個(gè)數(shù)字映射到0至4個(gè)字母。給定一個(gè)數(shù)字序列,實(shí)現(xiàn)一個(gè)算法來返回匹配單詞的列表。你會(huì)得到一張含有有效單詞的列表。映射如下圖所示:

示例 1:
輸入: num = "8733", words = ["tree", "used"]
輸出: ["tree", "used"]
示例 2:
輸入: num = "2", words = ["a", "b", "c", "d"]
輸出: ["a", "b", "c"]

提示:
num.length <= 1000
words.length <= 500
words[i].length == num.length
num中不會(huì)出現(xiàn) 0, 1 這兩個(gè)數(shù)字

思路

  1. 字典法,我們把每個(gè)數(shù)字可能的字符存下來,遍歷words數(shù)組,再看每個(gè)word的字符是否匹配輸入的數(shù)字。
    PS:和前綴樹有點(diǎn)類似,num的第i個(gè)數(shù)字就是前綴樹的第i層。

題解

/**
 * @Author: vividzcs
 * @Date: 2021/2/28 11:20 上午
 */
public class GetValidT9Words {
    public static void main(String[] args) {
        GetValidT9Words instance = new GetValidT9Words();
        String num = "8733";
        String[] words = {"tree", "used"};
        List<String> result = instance.getValidT9Words(num, words);
        System.out.println(result);
    }

    /**
     * 執(zhí)行用時(shí):5 ms, 在所有 Java 提交中擊敗了80.99%的用戶
     * 內(nèi)存消耗:40.4 MB, 在所有 Java 提交中擊敗了38.80%的用戶
     */
    public List<String> getValidT9Words(String num, String[] words) {
        Map<Character, Set<Character>> dict = getMap();
        List<String> result = new ArrayList<>();
        for (int i=0; i<words.length; i++) {
            String word = words[i];
            if (!matchWord(dict, word, num)) {
                continue;
            }
            result.add(word);
        }

        return result;
    }

    private boolean matchWord(Map<Character, Set<Character>> dict, String word, String num) {
        for (int j=0; j<word.length(); j++) {
            Character c = word.charAt(j);
            if (!dict.get(num.charAt(j)).contains(c)) {
                return false;
            }
        }
        return true;
    }
    private Map<Character, Set<Character>> getMap() {
        Map<Character, Set<Character>> dict = new HashMap<>();
        dict.put('2', new HashSet<>(Arrays.asList('a','b','c')));
        dict.put('3', new HashSet<>(Arrays.asList('d','e','f')));
        dict.put('4', new HashSet<>(Arrays.asList('g','h','i')));
        dict.put('5', new HashSet<>(Arrays.asList('j','k','l')));
        dict.put('6', new HashSet<>(Arrays.asList('m','n','o')));
        dict.put('7', new HashSet<>(Arrays.asList('p','q','r', 's')));
        dict.put('8', new HashSet<>(Arrays.asList('t','u','v')));
        dict.put('9', new HashSet<>(Arrays.asList('w','x','y','z')));
        return dict;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、 iOS為什么必須在主線程操作UI?[https://778477.github.io/2017/06/19/...
    菲特峰閱讀 424評(píng)論 0 0
  • LeetCode題目精選 兩數(shù)之和鏈接:https://leetcode-cn.com/problems/two-...
    奔跑小電驢閱讀 249評(píng)論 0 0
  • 兩數(shù)之和 暴力法或者map一遍掃描 兩數(shù)相加 用鏈表來模擬加法。 無重復(fù)字符的最長(zhǎng)子串 雙指針操作,模板題目。 Z...
    周飛飛飛機(jī)閱讀 979評(píng)論 0 0
  • 1 多益網(wǎng)絡(luò)面試 Q:博客項(xiàng)目里面如何驗(yàn)證賬號(hào)密碼的?有沒有做什么安全措施 A: 在登錄表單中填寫用戶名和密碼后,...
    全村希望gone閱讀 996評(píng)論 0 3
  • 《算法練習(xí)-文章匯總》[http://www.itdecent.cn/p/fc7c0e8cc5cb] Day1:...
    一畝三分甜閱讀 808評(píng)論 0 0

友情鏈接更多精彩內(nèi)容