LeetCode 290. Word Pattern

题目

很简单的字符串处理题

class Solution {
public:
    map<string, char> m;
map<char, string> m2;
bool wordPattern(string pattern, string str) {

    string s = "";
    int i,j;
    for ( i = 0, j = 0;i <= str.length() && j < pattern.length();i++)
    {
        if ((i==str.length()||str[i] == ' ')&& s!="")
        {
            if (m.count(s) != 0)
            {
                if (m[s] != pattern[j])
                    return false;
            }
            if (m2.count(pattern[j]) != 0)
            {
                if (m2[pattern[j]] != s)
                    return false;
            }

            m[s] = pattern[j];
            m2[pattern[j]] = s;

            s = "";
            j++;
            continue;
        }

        if (str[i] != ' ')
        {
            s += str[i];
        }

    }
    
    if(i!=str.length()+1||j!=pattern.length())
        return false;
    return true;

}
};
posted @ 2020-05-26 19:53  Shendu.CC  阅读(102)  评论(0编辑  收藏  举报