zoukankan      html  css  js  c++  java
  • 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;
    
    }
    };
    
  • 相关阅读:
    【leetcode】对称二叉树
    【leetcode】判断回文数
    053686
    053685
    053684
    053683
    053682
    053681
    053680
    053477
  • 原文地址:https://www.cnblogs.com/dacc123/p/12968308.html
Copyright © 2011-2022 走看看