Leetcode-290 Word Pattern(单词模式)

 1 class Solution
 2 {
 3     public:
 4         int getWordEnd;
 5         string getWord(string str)
 6         {
 7             string tmp;
 8             int i;
 9             for(i = getWordEnd;str[i] != ' '&& str[i]!='\0';i ++)
10             {
11                 tmp += str[i];
12             }
13             getWordEnd = i+1;
14             return tmp;
15         }
16         bool wordPattern(string pattern, string str)
17         {
18             map<char,string> m;
19             map<string,char> m2;
20             getWordEnd = 0;
21             for(int i = 0;i < pattern.size();i ++)
22             {
23                 string word = getWord(str);
24                 if(!m.count(pattern[i]))
25                 {
26                     m[pattern[i]] = word;
27                     if(!m2.count(word))
28                     {
29                         m2[word] = pattern[i];
30                     }
31                     else
32                     {
33                         return false;
34                     }
35                 }
36                 else if(m[pattern[i]] != word)
37                 {
38                     cout << i <<endl;
39                     return false;
40                 }
41             }
42 
43             if(getWordEnd-1 != str.size())
44                 return false;
45             return true;
46         }
47 };

 

posted @ 2018-10-18 11:36  Asurudo  阅读(142)  评论(0编辑  收藏  举报