LintCode-231: Typeahead (System Design题)

此题考察对map的应用。
map < string, vector > wordMap;
记录每个subWord对应的dict中的名字。
vector words;
把dict中的名字按顺序记下来,方便search()返回。

注意去重。有两种去重的情况。
一个是dict里面单个名字出现两个同样的字母或同样的单词时,比如说tree,或”Yang Yang”。这种情况通过
if (wordMap[subWord][vecSize - 1] != index)
来去重。
另一种情况是dict里面出现两个一样的名字,比如说”James Wang”, “James Wang”,这种情况map自身就能去重。

class Typeahead {
public:
    /*
    * @param dict: A dictionary of words dict
    */
    map<string, vector<int>> wordMap;   //the vector<int> stores the index of the strings in dict.
    vector<string> words; // e.g., {"Jason Zhang", James Yu", ...}
    Typeahead(unordered_set<string> dict) {
        int index = 0;
        for (auto str : dict) {
            words.push_back(str); 
            //split the string into subWords
            int strSize = str.size();
            for (int i = 0; i < strSize; ++i) {
                for (int j = i; j < strSize; ++j) {
                    string subWord = str.substr(i, j - i + 1);
                    if (wordMap.find(subWord) == wordMap.end()) {
                        wordMap[subWord] = vector<int>(1, index);   //generate {index}
                    } else {
                        //remove duplicate: make sure that "Tree" will not enter into the vec twice as it has two 'e'.
                        int vecSize = wordMap[subWord].size();
                        if (wordMap[subWord][vecSize - 1] != index) {
                            wordMap[subWord].push_back(index);
                        }
                    }
                }
            }
            index++;
        }
    }

    /*
     * @param str: a string
     * @return: a list of words
     */
    vector<string> search(string &str) {
        vector<string> result;

        if (wordMap.find(str) == wordMap.end()) {
            return result;
        } else {
            for (auto i : wordMap[str]) {
                result.push_back(words[i]);
            }
        }      
        return result;
    }
};

如果这题的search是输入单个first name或last name,则可以简化用stringstream或split()函数来做。代码如下:

    Typeahead(unordered_set<string> dict) {
        int index = 0;
        for (auto str : dict) {
            words.push_back(str); 
            //split the string into words
            stringstream ss(str);
            string buf; //stores the first name or last name like "Jason", "Zhang", etc
            while(ss >> buf) {
                if (wordMap.find(buf) == wordMap.end()) {
                    wordMap[buf] = vector<int>(1, index);   //generate {index}
                } else {
                    wordMap[buf].push_back(index);
                }
            }

            index++;
        }
    }

另外,这题不知道能不能用Trie做。下次再想想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值