zoukankan      html  css  js  c++  java
  • Xiaohe-LeetCode 288 Unique Word Abbreviation

    This question is confusing. But if we see the wrong cases below, we can understand the meaning.

    If word's abbr is not in dic, then return true.

    Else if word's abbr is in dic 

      {The matching words in dic are all exactly the same word to the input word: return true. else return false}

    Else return false;

    (1)Right Solution:(need to find better solution)

    59.64%,256ms. The interesting parts is that if I use d.size() instead of d.length() here, the efficiency will drop down to 

    292 ms, 29.2%.

    class ValidWordAbbr {
    public:
    ValidWordAbbr(vector<string> &dictionary) {
    for (string& d : dictionary) {
    int n = d.length();
    string abbr = d[0] + to_string(n) + d[n - 1];
    mp[abbr].insert(d);
    }
    }

    bool isUnique(string word) {
    int n = word.length();
    string abbr = word[0] + to_string(n) + word[n - 1];
    return mp[abbr].count(word) == mp[abbr].size();
    }
    private:
    unordered_map<string, unordered_set<string>> mp;
    };

    (2) Wrong Case:

    Input:["dog"],isUnique("dig"),isUnique("dug"),isUnique("dag"),isUnique("dog"),isUnique("doge")
    Output:[true,true,true,true,true]
    Expected:[false,false,false,true,true]
     
    Input:[],isUnique("hello")
    Output:[false]
    Expected:[true]
     
    Input:["hello"],isUnique("hello")
    Output:[false]
    Expected:[true]
     
    Input:["a","a"],isUnique("a")
    Output:[false]
    Expected:[true]
     
  • 相关阅读:
    Java设计模式—模板方法模式
    STM32 常用GPIO操作函数记录
    GPIO 配置之ODR, BSRR, BRR 详解
    STM32F4先设置寄存器还是先使能时钟
    LDR指令的格式:
    printf函数重定向
    stm32F4各个库文件的作用分析
    STM32F4时钟设置分析
    STM32F407存储器和总线架构
    SPI移位寄存器
  • 原文地址:https://www.cnblogs.com/CathyXiaohe/p/4994498.html
Copyright © 2011-2022 走看看