LeetCode //C - 290. Word Pattern
290. Word Pattern
Given a pattern and a string s, find if s 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 s.
Example 1:
Input: pattern = “abba”, s = “dog cat cat dog”
Output: true
Example 2:
Input: pattern = “abba”, s = “dog cat cat fish”
Output: false
Example 3:
Input: pattern = “aaaa”, s = “dog cat cat dog”
Output: false
Constraints:
- 1 <= pattern.length <= 300
- pattern contains only lower-case English letters.
- 1 <= s.length <= 3000
- s contains only lowercase English letters and spaces ’ '.
- s does not contain any leading or trailing spaces.
- All the words in s are separated by a single space.
From: LeetCode
Link: 290. Word Pattern
Solution:
Ideas:
The code for the wordPattern function is designed to determine whether a given string s follows a specific pattern. Here’s a breakdown of the idea behind the code:
-
Initialization: The code initializes an array of pointers words, where each index corresponds to a lowercase letter in the pattern (from ‘a’ to ‘z’). This array will be used to track the mapping between each character in the pattern and a corresponding word in the string s.
-
String Copy: Since the code utilizes strtok to tokenize the string s, which alters the original string, a copy of the string is made to preserve the input.
-
Tokenization and Mapping: The code tokenizes the string s into words and iterates through these words alongside the characters in the pattern:
- If the pattern is exhausted before all words are processed, the function returns false, as there is a mismatch in length.
- For each character in the pattern, the code checks whether it has been mapped to a word before. If not, it verifies that no other character has been mapped to the current word (to ensure a bijection). If all is well, the current character is mapped to the current word.
- If the character has already been mapped to a word, the code checks that this word matches the current word in the string. If there’s a mismatch, the function returns false.
-
Length Check: After processing all words, the code checks whether the pattern’s length matches the number of words. If there’s a mismatch, the function returns false.
-
Result: If all checks pass, the function returns true, indicating that the string s follows the given pattern.
Code:
bool wordPattern(char * pattern, char * s) {char *words[26];for (int i = 0; i < 26; i++) words[i] = NULL;char *s_copy = strdup(s);char *token = strtok(s_copy, " ");int i = 0;while (token != NULL) {// If the pattern is shorter than the number of words, return falseif (i >= strlen(pattern)) {free(s_copy);return false;}int idx = pattern[i] - 'a';if (words[idx] == NULL) {// Check if any other character maps to the same wordfor (int j = 0; j < 26; j++) {if (words[j] && strcmp(words[j], token) == 0) {free(s_copy);return false;}}words[idx] = token;} else {if (strcmp(words[idx], token) != 0) {free(s_copy);return false; // Mismatch in mapping}}token = strtok(NULL, " ");i++;}free(s_copy);// Check if pattern length matches the number of wordsif (i != strlen(pattern)) return false;return true;
}
相关文章:
![](https://www.ngui.cc/images/no-images.jpg)
LeetCode //C - 290. Word Pattern
290. Word Pattern Given a pattern and a string s, find if s 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 s. Example 1: Input: pattern “abba”, s “dog c…...
![](https://img-blog.csdnimg.cn/334a300fd3d04f6f9bee541b7cbeed9f.png)
[保研/考研机试] 括号匹配问题 C++实现
题目描述: 在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来的字符串&am…...
![](https://img-blog.csdnimg.cn/56ce8601aa9b444fbdd699dfa8a66080.png)
springBoot集成caffeine,自定义缓存配置 CacheManager
目录 springboot集成caffeine Maven依赖 配置信息:properties文件 config配置 使用案例 Caffeine定制化配置多个cachemanager springboot集成redis并且定制化配置cachemanager springboot集成caffeine Caffeine是一种基于服务器内存的缓存库。它将数据存储在…...
![](https://img-blog.csdnimg.cn/521646f4450146939b60adc71609e4b9.png)
【瑞吉外卖】Git部分学习
Git简介 Git是一个分布式版本控制工具,通常用来对软件开发过程中的源代码文件进行管理。通过Git仓库来存储和管理这些文件,Git仓库分为两种: 本地仓库:开发人员自己电脑上的Git仓库 远程仓库:远程服务器上的Git仓库…...
![](https://www.ngui.cc/images/no-images.jpg)
如何阐述自己做了一个什么样的东西
线上qps2000,主要的性能瓶颈在于出现在数据库I/O上。另外,如果是一个正常部署的容器,qps能达到几百就不错了。资讯服务现在做了静态的底层页,所以热点新闻多数会命中底层页,即便没有命中底层页,也会走多层的…...
![](https://www.ngui.cc/images/no-images.jpg)
TC3XX - MCAL知识点(二十二):QSPI 同步与异步 Mcal配置及代码实战
目录 1、MCAL配置 1.1、配置目标 1.2、同步QSPI配置 1.2.1、SpiGeneral 1.2.2、SpiMaxChannel 1.2.3、SpiMaxJob...
![](https://img-blog.csdnimg.cn/img_convert/2222037583dfd54ac21fcf6c4bf006f6.png)
led台灯哪些牌子性价比高?推荐几款性价比高的护眼台灯
作为学龄期儿童的家长,最担心的就是孩子长时间学习影响视力健康。无论是上网课、写作业、玩桌游还是陪伴孩子读绘本,都需要一个足够明亮的照明环境,因此选购一款为孩子视力发展保驾护航的台灯非常重要。为大家推荐几款性价比高的护眼台灯。 …...
![](https://www.ngui.cc/images/no-images.jpg)
什么情况下容易发生锁表及如何处理
目录 什么情况下容易发生锁表发生锁表怎么解决 什么情况下容易发生锁表 在数据库中,当多个事务同时竞争访问同一个表的资源时,可能会发生锁表现象,导致性能下降甚至阻塞。以下情况容易导致锁表问题: 大事务操作:如果一…...
![](https://img-blog.csdnimg.cn/e90adc1f769748c99cbf640e205422c1.png)
elk开启组件监控
elk开启组件监控 效果: logstash配置 /etc/logstash/logstash.yml rootnode1:~# grep -Ev "^#|^$" /etc/logstash/logstash.yml path.data: /var/lib/logstash path.logs: /var/log/logstash xpack.monitoring.enabled: true xpack.monitoring.elasti…...
![](https://img-blog.csdnimg.cn/abec0da97bc4451fb1a2b975417dc32f.jpeg#pic_center)
Java Random 类的使用
Java中的Random类是用来生成伪随机数的工具类。它可以用来生成随机的整数、浮点数和布尔值。以下是Java Random类的一些常见用法: 创建Random对象: Random random new Random();生成随机整数: int randomNumber random.nextInt(); // 生…...
![](https://img-blog.csdnimg.cn/46aba8174d604ccc9a6d9eff62d2a7b2.png)
完美的分布式监控系统——Prometheus(普罗米修斯)与优雅的开源可视化平台——Grafana(格鲁夫娜)
一、基本概念 1、之间的关系 prometheus与grafana之间是相辅相成的关系。作为完美的分布式监控系统的Prometheus,就想布加迪威龙一样示例和动力强劲。在猛的车也少不了仪表盘来观察。于是优雅的可视化平台Grafana出现了。 简而言之Grafana作为可视化的平台ÿ…...
![](https://img-blog.csdnimg.cn/6c5b8e2f4b6347b4a5dffe9a65614b53.png)
pycharm的Terminal中如何设置打开anaconda3的虚拟环境
在pycharm的File -> Settings -> Tools -> Terminal下面,如下图所示 修改为红框中内容,然后关闭终端在重新打开终端,即可看到anaconda3的虚拟环境就已经会被更新...
![](https://img-blog.csdnimg.cn/c1cd80f8473e48e39d2b36fb95a30378.png)
Jmeter(四) - 从入门到精通 - 创建网络测试计划(详解教程)
1.简介 在本节中,您将学习如何创建基本的 测试计划来测试网站。您将创建五个用户,这些用户将请求发送到JMeter网站上的两个页面。另外,您将告诉用户两次运行测试。因此,请求总数为(5个用户)x(2…...
![](https://img-blog.csdnimg.cn/10dc605fd8c4478398e7ed3be4bf7899.png)
Flowable-结束事件-空结束事件
目录 定义图形标记XML内容 定义 空结束事件是最常见的一种结束事件,也是最简单的一种结束事件,只要把结束任务置于流程 或分支的最后节点,流程实例运行到该节点的时候,流程引擎就会结束该流程实例或分支。前面提 到,结…...
![](https://img-blog.csdnimg.cn/6bcc2aca3f6a4a0ca36991dbe259314e.png)
Elasticsearch:如何创建 Elasticsearch PEM 和/或 P12 证书?
你是否希望使用 SSL/TLS 证书来保护你的 Elasticsearch 部署? 在本文中,我们将指导你完成为 Elasticsearch 创建 PEM 和 P12 证书的过程。 这些证书在建立安全连接和确保 Elasticsearch 集群的完整性方面发挥着至关重要的作用。 友情提示:你可…...
![](https://img-blog.csdnimg.cn/504599a4ebee4bc7a7d7121d41a9f900.png#pic_center)
数仓架构模型设计参考
1、数据技术架构 1.1、技术架构 1.2、数据分层 将数据仓库分为三层,自下而上为:数据引入层(ODS,Operation Data Store)、数据公共层(CDM,Common Data Model)和数据应用层ÿ…...
![](https://www.ngui.cc/images/no-images.jpg)
RedisTemplate.opsForGeo()用法简介并举例
RedisTemplate.opsForGeo()是RedisTemplate类提供的用于操作Geo类型(地理位置)的方法。它可以用于对Redis中的Geo数据结构进行各种操作,如添加地理位置、获取距离、获取位置信息等。 下面是一些常用的RedisTemplate.opsForGeo()方法及其用法…...
![](https://img-blog.csdnimg.cn/1423b68274fd42a5a0b288922d07887f.png)
Android OkHttp源码分析--拦截器
拦截器责任链: OkHttp最核心的工作是在 getResponseWithInterceptorChain() 中进行,在进入这个方法分析之前,我们先来了 解什么是责任链模式,因为此方法就是利用的责任链模式完成一步步的请求。 拦截器流程: OkHtt…...
![](https://www.ngui.cc/images/no-images.jpg)
docker:如何传环境变量给entrypoint
使用shell,不带中括号 ENTRYPOINT .\main -web -c $CONFIGENTRYPOINT [sh, -c, ".\main -web -c $CONFIG"]docker build --build-arg ENVIROMENTintegration // 覆盖ENTRYPOINT命令 使用shell脚本 ENTRYPOINT ["./entrypoint.sh"]entrypoint.sh 镜像是a…...
![](https://img-blog.csdnimg.cn/80c65153d8f447faad66f93158494848.png)
kuboard安装和使用
windows平台下使用docker和docker-compose部署Kuboard,并添加Docker Desktop for windows的k8s单机集群 使用docker安装 docker run -d \--restartunless-stopped \--namekuboard \-p 80:80/tcp \-p 10081:10081/tcp \-e KUBOARD_ENDPOINT"http://内网IP:80&…...
![](https://img-blog.csdnimg.cn/114c4e2012094caba7a5d5ad517c48fb.png)
海外直播种草短视频购物网站巴西独立站搭建
一、市场调研 在搭建网站之前,需要进行充分的市场调研,了解巴西市场的消费者需求、购物习惯和竞争情况。可以通过以下途径进行市场调研: 调查问卷:可以在巴西市场上发放调查问卷,了解消费者的购物习惯、偏好、购买力…...
![](https://www.ngui.cc/images/no-images.jpg)
C#图像均值和方差计算实例
本文展示图像均值和方差计算实例,分别实现RGB图像和8位单通道图像的计算方法 实现代码如下: #region 方法 RGB图像均值 直接操作内存快/// <summary>/// 定义RGB图像均值函数/// </summary>/// <param name="bmp"></param>/// <retur…...
![](https://img-blog.csdnimg.cn/f5290f394efa4fa3bd9423fd1403511a.png#pic_center)
【动态规划】数字三角形
算法提高课课堂笔记。 文章目录 摘花生题意思路代码 最低通行费题意思路代码 方格取数题意思路代码 摘花生 题目链接 Hello Kitty想摘点花生送给她喜欢的米老鼠。 她来到一片有网格状道路的矩形花生地(如下图),从西北角进去,东南角出来。 地里每个道…...
![](https://img-blog.csdnimg.cn/6cbcd6c17cec4dba9bb3c0f895f02fa2.png)
【C++】开源:ceres和g2o非线性优化库配置使用
😏★,:.☆( ̄▽ ̄)/$:.★ 😏 这篇文章主要介绍ceres和g2o非线性优化库配置使用。 无专精则不能成,无涉猎则不能通。——梁启超 欢迎来到我的博客,一起学习,共同进步。 喜欢的朋友可以关注一下&…...
![](https://img-blog.csdnimg.cn/f605a81e9e2241cbbd1f58889d2bb4a5.png)
OCR学习
...
![](https://www.ngui.cc/images/no-images.jpg)
《练习100》56~60
题目56 M 5 a [1, 2, 3, 4, 5] i 1 j M - 1 while i < M:# print(f"第{i1}轮交换前:i {i}, j {j} , a[{i}] {a[i]} , a[{j}] {a[j]}")a[i], a[j] a[j], a[i]# print(f"第{i1}轮交换后:i {i}, j {j} , a[{i}] {a[i]} , a[…...
![](https://img-blog.csdnimg.cn/03645256e33b4027a6ec0b563d580fdb.png)
基于大数据为底层好用准确性高的竞彩足球比分预测进球数分析软件介绍推荐
大数据与贝叶斯理论在足球比赛分析与预测中的应用 随着科技的不断进步,大数据分析在各个领域的应用也越来越广泛,其中包括体育竞技。足球比赛作为全球最受欢迎的运动之一,也借助大数据和贝叶斯理论来进行模型分析和预测。本文将通过结合贝叶…...
![](https://img-blog.csdnimg.cn/bcb38dfa790d4c2aa71095d0fe322c77.png)
Django进阶
1.orm 1.1 基本操作 orm,关系对象映射。 类 --> SQL --> 表 对象 --> SQL --> 数据特点:开发效率高、执行效率低( 程序写的垃圾SQL )。 编写ORM操作的步骤: settings.py,连…...
![](https://www.ngui.cc/images/no-images.jpg)
Linux系统服务管理
服务命令比较 操作 Linux 6 Linux7 服务开机自动启动 chkconfig --level 35 iptables on systemctl enable firewalld.service 服务器开机不自动启动 chkconfig --level 35 iptables off systemctl disable firewalld.service 加入自定义服务 chkconfig --add aaa s…...
![](https://img-blog.csdnimg.cn/7ab0505e2dcb4eeb8bb3a3c8b54e7881.png)
C#之控制台版本得贪吃蛇
贪吃蛇小时候大家都玩过,具体步骤如下: 1.给游戏制造一个有限得空间。 2.生成墙壁,小蛇碰撞到墙壁或者咬到自己的尾巴,游戏结束。 3.生成随机的食物。 4.吃掉食物,增加自身的体长,并生成新的食物。 具体代码如下&…...
![](https://img-blog.csdnimg.cn/img_convert/0818b9ca8b590ca3270a3433284dd417.png)
qq云端服务器/seo推广优化排名软件
一、基本接口或类——>DriverManager:用于管理JDBC驱动的服务类。主要功能是获取Connection对象。——>Connection:代表数据库连接对象,每个Connection代表一个物理连接会话。——>Statement:用于执行SQL语句的工具接口。…...
![](/images/no-images.jpg)
网站css初始化/平台如何做推广
Lighting Box2源码分析(一):整体功能Lighting Box2源码分析(二):Sun ShaftsLighting Box2源码分析(三):Depth Of Field景深Lighting Box2源码分析(四):Global Fog全局雾...
![](/images/no-images.jpg)
园林景观设计公司需要什么资质/项目优化seo
IOS开发——自定义类归档(继承于自定义类) 我先创建一个新闻基类,对其实现NSCoding协议;再创建一个图组新闻类继承于这个新闻基类,添加一些属性(图组),那么这个自定义的图组新闻类该…...
![](https://cdn.luogu.org/upload/pic/1114.png)
个人房产查询系统网站官网/百度收录提交网站后多久收录
因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客。 先直接上题目然后贴代码,具体讲解都写代码里了。 参考的博客等的链接都贴代码里了,有空再好好写。 P2042 [NOI2005]维护数列 题目描述 请写一个程序,要求维…...
![](https://img-blog.csdnimg.cn/20190120110349997.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMDM2NzU0,size_16,color_FFFFFF,t_70)
美容院门户网站开发/百度关键词查询
Android Studio 3.0 之后Tools没有Android选项,想打开monitor查看文件就不行,解决方法: 这样即可找到虚拟机或者真机的文件。 如果是真机的文件,一定要记得小心哦。...
![](/images/no-images.jpg)
企业网站为什么打不开/seo在线优化技术
一、inner join 内联查询查出满足on条件的两个表的公共交集。使用inner join时 inner可以省略。 select * from Student inner join Score on Student.SNoScore.SNo 二、left join 左外联查询,left outer join的简写。以左边的表为基准与右边的表进行关联…...