一天一道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. ●BZOJ 3996 [TJOI2015]线性代数

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3996 题解: 好题啊.(不太熟悉矩阵相关,所以按某些博主的模型转换来理解的)首先,那个式子可 ...

  2. python txt文件的写入和读取

    1.文件的打开 使用open () 函数 打开文件.他有两个参数,文件路径或文件名和文件的打开方式. "r" 只读模式,不能编辑和删除文件内容. "w" 写入模 ...

  3. 动态规划--Kin

    动态规划: 1.最大子序列和 2.LIS最长递增子序列 3.LCS最长公共子序列 4.矩阵连乘 5.数字金字塔 1.最大子序列和 #include<iostream> using name ...

  4. CRM客户关系管理系统(一)

    第一章.CRM介绍和开发流程 1.1.CRM简介 客户关系管理(CRM) 客户关系管理(customer relationship management)的定义是:企业为提高核心竞争力,利用相应的信息 ...

  5. 螺旋打印2D数组

    //一破题付出血的代价 多思考!public static void offer(int [][]a){ ,right=a.length-,low=,high=a[].length-; while(l ...

  6. 实践详细篇-Windows下使用VS2015编译安装Caffe环境(CPU ONLY)

    学习深度学习背景 最近在做一款抢票软件,由于12306经常检测账号状态,抢票抢着抢着就需要重新登录了,然后登录是需要验证码的.所以我最开始是想到了使用java基于感知哈希算法pHash做相似度匹配识别 ...

  7. Java并发中的CopyOnWrite容器

    Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容然后再改, ...

  8. nginx模块,模块的配置使用

    nginx模块官方模块(默认支持的)第三方模块 1. --with-http_stub_status_module nginx的客户端状态 配置syntax: sub_status;default:- ...

  9. jquery插件存档

    1.选择插件selectMenu github地址:https://github.com/josiaho/selectMenu 2.选择插件bootstrap_multiselect 官方地址:htt ...

  10. Goland 提示 :configuration is still incorrect 的解决

    安装好 Goland 后,调试编译的时候提示 goland configuration is still incorrect,百度 和 Google 都没有明确答案 Google 上有一些提示,但是也 ...