前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 题目解析之 Word Pattern

Leetcode 题目解析之 Word Pattern

原创
作者头像
ruochen
发布于 2022-01-14 03:31:42
发布于 2022-01-14 03:31:42
1.4K00
代码可运行
举报
运行总次数: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.

保证1对1的映射

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    public boolean wordPattern(String pattern, String str) {
        if (pattern == null || str == null) {
            return false;
        }
        String[] strs = str.split(" ");
        if (pattern.length() != strs.length) {
            return false;
        }
        Map<Character, String> map = new HashMap<Character, String>();
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (map.containsKey(c)) {
                if (!map.get(c).equals(strs[i])) {
                    return false;
                }
            } else {
                // 保证1对1的映射
                if (map.containsValue(strs[i])) {
                    return false;
                }
                map.put(c, strs[i]);
            }
        }
        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
6220
LeetCode笔记:290. Word Pattern
这里的模型和字符串都是字符串,我们先全部转化为字母数组和字符串数组,方便进行比较。
Cloudox
2021/11/23
2010
【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
6690
【LeetCode 290】 关关的刷题日记28 Word Pattern
【算法千题案例】⚡️每日LeetCode打卡⚡️——65.单词规律
给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。
呆呆敲代码的小Y
2021/11/01
3990
【算法千题案例】⚡️每日LeetCode打卡⚡️——65.单词规律
【leetcode刷题】T41-单词模式
Given a pattern and a string str, find if str follows the same pattern.
木又AI帮
2019/07/17
4540
Baozi Training Leetcode Solution 290: Word Pattern
博客园:https://www.cnblogs.com/baozitraining/p/11087658.html
包子面试培训
2019/07/08
4960
Baozi Training Leetcode Solution 290: Word Pattern
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 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" shou
triplebee
2018/01/12
5130
LeetCode 0290 - Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Reck Zhang
2021/08/11
2290
290. Word Pattern(单词模式)(Integer的坑)
题目地址:https://leetcode.com/problems/word-pattern/description/
砖业洋__
2023/05/06
2130
290. Word Pattern(单词模式)(Integer的坑)
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
7670
Baozi Training Leetcode Solution 291: Word Pattern II
leetcode之单词规律
这里采用HashMap记录每个pattern跟文字的映射,不过这里要判断下pattern的个数与文字的个数,另外同一个value不能映射到不同的key。
code4it
2020/10/21
4180
leetcode之单词规律
String - 290. Word Pattern
Given two strings s and _t _, write a function to determine if t is an anagram of s.
ppxai
2020/09/23
3310
LeetCode 图解 | 290 . 单词规律
题目来源于 LeetCode 上第 290 号问题:单词规律。题目难度为 Easy,目前通过率为 42.4% 。
五分钟学算法
2020/04/24
3680
脚撕LeetCode(290)Easy
题目地址:https://leetcode-cn.com/problems/word-pattern/
JathonKatu
2021/06/10
2980
290. 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。
张伦聪zhangluncong
2022/10/26
1930
LeetCode — (1)
  Nim Game、WordPattern、Move zeros、First Bad version、Ugly Number五个算法的python实现。
oYabea
2020/09/07
4110
Leetcode 290. Word Pattern
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82594572
Tyan
2019/05/25
3320
Day9-字符串-字符模式匹配
Q:已知字符串pattern与字符串str,确认str是否与pattern匹配。str与pattern匹配代表字符串str中的单词与pattern中的字符一一对应。(其中pattern中只包含小写字符,str中
BUPTrenyi
2019/07/15
6400
Day9-字符串-字符模式匹配
词语模式_哈希表
已知字符串pattern与字符串str,确认str是否与pattern匹配。str与pattern匹配代表字符 串str中的单词与pattern中的字符一一对应。(其中pattern中只包含小写字符,str中的单词只包含小写字符,使用空格分隔。) 例如, pattern = “abba”, str = “dog cat cat dog” 匹配. pattern = “abba”, str = “dog cat cat fish” 不匹配. pattern = "aaaa", str = "dog cat cat dog"不匹配. pattern = "abba", str = "dog dog dog dog"不匹配. LeetCode 290. Word Pattern
小飞侠xp
2018/08/29
4220
相关推荐
leetcode-290-Word Pattern
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验