648. Replace Words(LeetCode)
Posted 无惧风云
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了648. Replace Words(LeetCode)相关的知识,希望对你有一定的参考价值。
In English, we have a concept called root
, which can be followed by some other words to form another longer word - let‘s call this word successor
. For example, the root an
, followed by other
, which can form another word another
.
Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor
in the sentence with theroot
forming it. If a successor
has many roots
can form it, replace it with the root with the shortest length.
You need to output the sentence after the replacement.
Example 1:
Input: dict = ["cat", "bat", "rat"] sentence = "the cattle was rattled by the battery" Output: "the cat was rat by the bat"
Note:
- The input will only have lower-case letters.
- 1 <= dict words number <= 1000
- 1 <= sentence words number <= 1000
- 1 <= root length <= 100
- 1 <= sentence words length <= 1000
1 class Solution { 2 public: 3 string replaceWords(vector<string>& dict, string sentence) { 4 if (sentence.size() == 0 || dict.size() == 0) 5 return sentence; 6 stringstream ss(sentence); 7 vector<string> vet; 8 string str; 9 while (ss >> str) 10 { 11 vet.push_back(str); 12 } 13 for (int i = 0; i < vet.size(); i++) 14 { 15 for (int j = 0; j < dict.size(); j++) 16 { 17 /*cout << vet[i] << vet[i].size() << " " << dict[j] << dict[j].size() << " 22" << vet[i].substr(0, dict[j].size()) << endl;*/ 18 19 if (vet[i].size() >= dict[j].size() && vet[i].substr(0, dict[j].size()) == dict[j]) 20 { 21 vet[i] = dict[j]; 22 /*cout <<"11111111"<< vet[i] << endl;*/ 23 } 24 } 25 } 26 string str1 = ""; 27 for (int i = 0; i < vet.size(); i++) 28 { 29 str1 += vet[i]; 30 if (i != vet.size()-1) 31 str1 += " "; 32 33 } 34 return str1; 35 } 36 };
别人用字典树做的如下:
1 class Solution { 2 private: 3 class TrieNode{ 4 public: 5 bool eow; 6 vector<TrieNode*> chars; 7 TrieNode():eow(false), chars(vector<TrieNode*>(26,NULL)){ 8 9 } 10 }; 11 12 class Trie{ 13 public: 14 TrieNode* head; 15 16 Trie():head(new TrieNode()) {} 17 18 bool isWord(string str){ 19 TrieNode* cur = head; 20 for (int i = 0; i < str.size(); i++){ 21 char c = str[i]; 22 int index = (c-‘a‘) %26; 23 if (cur->chars[index] == NULL){ 24 return false; 25 } 26 cur = cur->chars[index]; 27 if (index == str.size() - 1){ 28 return cur->eow; 29 } 30 } 31 } 32 33 void insertWord(string str){ 34 TrieNode* cur = head; 35 for (int i = 0; i < str.size(); i++){ 36 char c = str[i]; 37 int index = (c-‘a‘) %26; 38 if (cur->chars[index] == NULL){ 39 cur->chars[index] = new TrieNode(); 40 } 41 cur = cur->chars[index]; 42 if (i == str.size() - 1){ 43 cur->eow = true; 44 } 45 } 46 } 47 48 string getShortestPrefix(string str){ 49 TrieNode* cur = head; 50 for (int i = 0; i < str.size(); i++){ 51 char c = str[i]; 52 int index = (c-‘a‘) %26; 53 if (cur->chars[index] == NULL){ 54 return str; 55 } 56 if (cur->chars[index]->eow){ 57 return str.substr(0,i+1); 58 } 59 cur = cur->chars[index]; 60 } 61 return str; 62 } 63 }; 64 public: 65 string replaceWords(vector<string>& dict, string sentence) { 66 Trie trie; 67 for (string str : dict){ 68 trie.insertWord(str); 69 } 70 71 string ans = ""; 72 int index = 0, beg = 0; 73 while ((index = sentence.find(‘ ‘,beg)) != string::npos){ 74 ans += trie.getShortestPrefix(sentence.substr(beg, index - beg)) + " "; 75 beg = index + 1; 76 } 77 ans += trie.getShortestPrefix(sentence.substr(beg)); 78 return ans; 79 } 80 };
以上是关于648. Replace Words(LeetCode)的主要内容,如果未能解决你的问题,请参考以下文章