Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 290. Word Pattern

Leetcode 290. Word Pattern

作者头像
triplebee
发布于 2018-03-27 08:53:55
发布于 2018-03-27 08:53:55
67500
代码可运行
举报
运行总次数:0
代码可运行

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:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. 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.

判断字符串是否是模式串的形式。

对字符串进行分词,用map保存和模式串字符的对应关系

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    bool wordPattern(string pattern, string str) {
        str += " ";
        int j = 0;
        unordered_map<string, string> mp;
        for(int i = 0; i < str.size(); i++)
        {
            int pos = str.find(" ", i);
            string tmp = str.substr(i, pos - i);
            i = pos;
            if(j == pattern.size()) return false;
            string key(pattern[j] + "\0");
            if(mp.find(key) == mp.end())
            {
                if(mp.find(tmp) == mp.end()) 
                {
                    mp[tmp] = key;
                    mp[key] = tmp;
                }
                else return false;
            }
            else if(mp[key] != tmp) return false;
            j++;
        }
        if(j != pattern.size()) return false;
        return true;
    }
};         
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
leetcode-290-Word Pattern
题目描述: 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
chenjx85
2018/05/21
6240
【LeetCode 290】 关关的刷题日记28 Word Pattern
关关的刷题日记28 – Leetcode 290. Word Pattern 题目 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 = "a
WZEARW
2018/04/10
6700
【LeetCode 290】 关关的刷题日记28 Word Pattern
LeetCode笔记:290. Word Pattern
这里的模型和字符串都是字符串,我们先全部转化为字母数组和字符串数组,方便进行比较。
Cloudox
2021/11/23
2020
LeetCode 0290 - Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Reck Zhang
2021/08/11
2330
Baozi Training Leetcode Solution 290: Word Pattern
博客园:https://www.cnblogs.com/baozitraining/p/11087658.html
包子面试培训
2019/07/08
4970
Baozi Training Leetcode Solution 290: Word Pattern
Leetcode 题目解析之 Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
ruochen
2022/01/14
1.4K0
290. Word Pattern(单词模式)(Integer的坑)
题目地址:https://leetcode.com/problems/word-pattern/description/
砖业洋__
2023/05/06
2140
290. Word Pattern(单词模式)(Integer的坑)
Leetcode 290. Word Pattern
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82594572
Tyan
2019/05/25
3330
LeetCode — (1)
  Nim Game、WordPattern、Move zeros、First Bad version、Ugly Number五个算法的python实现。
oYabea
2020/09/07
4120
290. 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。
张伦聪zhangluncong
2022/10/26
1950
LeetCode 290. 单词规律(哈希)
给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。
Michael阿明
2020/07/13
3210
LeetCode 290. 单词规律(哈希)
脚撕LeetCode(290)Easy
题目地址:https://leetcode-cn.com/problems/word-pattern/
JathonKatu
2021/06/10
2980
leetcode每日一题:290. 单词规律
leetcode每日一题:290. 单词规律:https://leetcode-cn.com/problems/word-pattern/
用户7685359
2020/12/22
2790
LeetCode 图解 | 290 . 单词规律
题目来源于 LeetCode 上第 290 号问题:单词规律。题目难度为 Easy,目前通过率为 42.4% 。
五分钟学算法
2020/04/24
3680
【leetcode刷题】T41-单词模式
Given a pattern and a string str, find if str follows the same pattern.
木又AI帮
2019/07/17
4560
【算法千题案例】⚡️每日LeetCode打卡⚡️——65.单词规律
给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。
呆呆敲代码的小Y
2021/11/01
4010
【算法千题案例】⚡️每日LeetCode打卡⚡️——65.单词规律
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==s
ShenduCC
2020/05/27
2770
哈希表问题-LeetCode 146、290、299、300(哈希表,双向链表,最小上升序列)
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作:获取数据 get 和 写入数据 put 。
算法工程师之路
2019/11/26
6100
Baozi Training Leetcode Solution 291: Word Pattern II
Blogger: https://blog.baozitraining.org/2019/07/leetcode-solution-291-word-pattern-ii.html
包子面试培训
2019/07/17
7690
Baozi Training Leetcode Solution 291: Word Pattern II
leetcode每日一题:290. 单词规律
题目链接:https://leetcode-cn.com/problems/word-pattern
用户3578099
2020/12/30
4700
相关推荐
leetcode-290-Word Pattern
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档