当前位置: 首页 > news >正文

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:

  1. 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.

  2. 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.

  3. 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.
  1. 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.

  2. 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;
}
http://www.lryc.cn/news/115490.html

相关文章:

  • [保研/考研机试] 括号匹配问题 C++实现
  • springBoot集成caffeine,自定义缓存配置 CacheManager
  • 【瑞吉外卖】Git部分学习
  • 如何阐述自己做了一个什么样的东西
  • TC3XX - MCAL知识点(二十二):QSPI 同步与异步 Mcal配置及代码实战
  • led台灯哪些牌子性价比高?推荐几款性价比高的护眼台灯
  • 什么情况下容易发生锁表及如何处理
  • elk开启组件监控
  • Java Random 类的使用
  • 完美的分布式监控系统——Prometheus(普罗米修斯)与优雅的开源可视化平台——Grafana(格鲁夫娜)
  • pycharm的Terminal中如何设置打开anaconda3的虚拟环境
  • Jmeter(四) - 从入门到精通 - 创建网络测试计划(详解教程)
  • Flowable-结束事件-空结束事件
  • Elasticsearch:如何创建 Elasticsearch PEM 和/或 P12 证书?
  • 数仓架构模型设计参考
  • RedisTemplate.opsForGeo()用法简介并举例
  • Android OkHttp源码分析--拦截器
  • docker:如何传环境变量给entrypoint
  • kuboard安装和使用
  • 海外直播种草短视频购物网站巴西独立站搭建
  • C#图像均值和方差计算实例
  • 【动态规划】数字三角形
  • 【C++】开源:ceres和g2o非线性优化库配置使用
  • OCR学习
  • 《练习100》56~60
  • 基于大数据为底层好用准确性高的竞彩足球比分预测进球数分析软件介绍推荐
  • Django进阶
  • Linux系统服务管理
  • C#之控制台版本得贪吃蛇
  • ffplay数据结构分析(一)