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;

}
};
原文地址:https://www.cnblogs.com/dacc123/p/12968308.html