一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples:

  • pattern = “abba”, str = “dog cat cat dog” should return true.
  • pattern = “abba”, str = “dog cat cat fish” should return false.
  • pattern = “aaaa”, str = “dog cat cat dog” should return false.
  • pattern = “abba”, str = “dog dog dog dog” should return false.

Notes:

  • You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

(二)解题

题目大意:字符串模式匹配。给定模板字符串pattern和待匹配字符串str,判断str是否与pattern模式匹配

解题思路:这是一道典型应用hash表求解的题。

利用两个辅助的hash表实现字符串的双向匹配。例如a->dog,dog->a

不允许出现模式中两个不同的字符对应str中同一个单词。

具体思路见代码:

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        int lenp=pattern.length();
        int lens=str.length();
        int i = 0;
        int j = 0;
        unordered_map<string,char> hash;//两个hash表,双向匹配
        unordered_map<char,string> hash2;
        while(i<lenp&&j<lens){
            string temp;
            while(j<lens&&str[j]!=' '){//找出str中以空格分隔的字符串
                temp+=str[j++];
            }
            auto iter = hash.find(temp);
            auto iter2 = hash2.find(pattern[i]);
            if(iter==hash.end()&&iter2==hash2.end()){//如果都不存在
                hash[temp] = pattern[i];
                hash2[pattern[i]] = temp;//记录双向匹配关系
            }
            else{
                if(hash[temp]==pattern[i]&&hash2[pattern[i]]==temp) {}
                else return false;//匹配不上
            }
            i++;j++;
        }
        return i>=lenp?j>=lens?true:false:false;//最后需要判断两个字符串是否匹配完
    }
};

【一天一道LeetCode】#290. Word Pattern的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  3. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  4. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  5. LeetCode 290. Word Pattern (词语模式)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  6. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  7. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

  8. Java [Leetcode 290]Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  9. leetcode 290 Word Pattern(map的应用)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  10. [leetcode] 290. Word Pattern (easy)

    原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...

随机推荐

  1. WebStorm配置node.js调试

    最近因为工作关系,一直在做node.js的开发,学习了koa框架,orm框架sequelize,以及swagger文档的配置.但是,最近因为swagger文档使用了es6的修饰器那么个东西(在java ...

  2. CCA更新流程分析

    1 CCA CCA(空间信道评估)在CSMA/CA中比较非常重要,事关整机吞吐量,所以对其实现进行简单分析.CCA好像应该有2种:CCA-CS,是属于PLCP层的,捕获到能量且能量值高于-82dB后, ...

  3. SpringCloud学习之feign

    一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...

  4. Linux允许、禁止ping包

    默认情况下Linux系统允许ping,但是在某些情况下为了安全起见,我们都把服务器设置为禁ping  临时允许ping命令可使用命令: echo 0 >/proc/sys/net/ipv4/ic ...

  5. 在Cisco Catalyst 3750端口做策略限速 QOS

    今天任务是在3750上限制端口的速率,本来以为是很简单的事,speed命令搞定,敲进去才知道speed命令只能叫端口速率改成10M或100M,也就是说只能起到端口高低速率的切换功能,不能自定义速率,后 ...

  6. 使用VMware Converter Standalone Client进行虚拟机 P2V提示 权限不足,无法连接\\ip\admin$的解决方法集锦

    使用VMware vCenter Converter Standalone Client进行虚拟机 P2V提示 权限不足,无法连接\\ip\admin$的解决方法集锦 步骤一 检查 远程桌面到&quo ...

  7. 各种异常 及异常类和Object类

    Day05 异常 Object类 equals方法,用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较.Object类中的equals方法内部使用的就是==比较运算符. 2. 描述人这个 ...

  8. C/C++知识大纲(只有作者看得懂的笔记)

    c标准库部分冷门函数. stdio.h char buf[256]; sscanf(buf,"%s",&buf); sprintf(buf,"Name: %s&q ...

  9. python笔记十五(面向对象及其特性)

    一.面向对象: class(类):一类拥有共同属性对象的抽象:定义了这些对象的属性和方法object(对象):是一个类实例化后的实例,类必须经过实例化才可以在程序中调用: 由于之前学习过java,对类 ...

  10. RxJava(10-操作符原理&自定义操作符)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51791120 本文出自:[openXu的博客] 目录: 自定义创建操作符 数据序列操作符li ...