问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3778 访问。

给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。

这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。

输入: pattern = "abba", str = "dog cat cat dog"

输出: true

输入:pattern = "abba", str = "dog cat cat fish"

输出: false

输入: pattern = "aaaa", str = "dog cat cat dog"

输出: false

输入: pattern = "abba", str = "dog dog dog dog"

输出: false

说明:你可以假设 pattern 只包含小写字母, str 包含了由单个空格分隔的小写字母。


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.

Input: pattern = "abba", str = "dog cat cat dog"

Output: true

Input:pattern = "abba", str = "dog cat cat fish"

Output: false

Input: pattern = "aaaa", str = "dog cat cat dog"

Output: false

Input: pattern = "abba", str = "dog dog dog dog"

Output: false

Notes:You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3778 访问。

public class Program {

    public static void Main(string[] args) {
string pattern = "abbc";
string str = "dog cat cat fish"; var res = WordPattern(pattern, str);
Console.WriteLine(res); Console.ReadKey();
} private static bool WordPattern(string pattern, string str) {
//哈希法
//总体思路是建立“模式-单词”键值
//按“模式”完全建立键值对
//a->dog,b->cat,b->(none),c->fish
//再还原式匹配单词
var dic = new Dictionary<int, string>();
//分隔单词
var words = str.Split(' ');
//长度不同时,直接return false
if(words.Length != pattern.Length) return false;
//用词典(哈希)记录每种“模式”对应的值
for(var i = 0; i < pattern.Length; i++) {
//如果已经存在这个键了,不管
//如果不存在这个键,则加入词典
if(!dic.ContainsKey(pattern[i])) {
//不存在这个键,却存在相应的值,明显模式不匹配
if(dic.ContainsValue(words[i])) return false;
//加入词典中
dic[pattern[i]] = words[i];
}
}
//比对模式和单词
for(var i = 0; i < pattern.Length; i++) {
//不同时,匹配失败
if(dic[pattern[i]] != words[i]) return false;
}
//匹配成功
return true;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3778 访问。

False

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#290-单词模式(Word Pattern)的更多相关文章

  1. LeetCode 290. 单词规律(Word Pattern) 41

    290. 单词规律 290. Word Pattern 题目描述 给定一种规律 pattern 和一个字符串 str,判断 str 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如,pattern ...

  2. [Swift]LeetCode290. 单词模式 | Word Pattern

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  3. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  4. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

  5. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  6. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  8. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  9. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

随机推荐

  1. IDEA 导入maven 项目,出现报错,不能运行之类的

    选择pom.xml,右键选择 Add as Maven Project

  2. OSCP Learning Notes - WebApp Exploitation(1)

    Installing XSS&MySQL FILE Download the Pentester Lab: XSS and MySQL FILE from the following webs ...

  3. Python Ethical Hacking - Malware Packaging(3)

    Convert Python Programs to OS X Executables https://files.pythonhosted.org/packages/4a/08/6ca123073a ...

  4. 集训作业 洛谷P1469 找筷子

    这个题的代码真的是短的不得了呢. 有个神奇的东西叫异或,写起来是这个样子的^. 这个东西可以查看2个数的二进制某位是否相同,相同取0,不同取1.虽然我用的不熟,但我可以想出来,如果2个相同的数异或,答 ...

  5. 【计网】图解HTTP常见知识点总结

    目录 目录 目录 初识TCP/IP TCP/IP协议族4层模型 初识HTTP 请求和响应 HTTP报文 HTTP状态码 HTTP报文首部 其他的首部字段 确保WEB安全的HTTPS HTTPS工作原理 ...

  6. 面试高频SpringMVC执行流程最优解(源码分析)

    文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star! 搜索关注微信公众号 码出Offer 领取各种学习资料! SpringMVC执行流程 SpringMVC概述 Spri ...

  7. 占个坑 未来学qt的时候专用

    今天看了一个大佬发了一个上位机图片便向大佬问道 ”上位机是用什么软件做的“大佬抛下一句qt ,在业界内很通用,windows和linux通吃,便让我萌生了一个想法,去学qt.虽说上位机时常听到,但是自 ...

  8. vue学习(十六) 自定义私有过滤器 ES6字符串新方法 填充字符串

    <div id="app"> <p>{{data | formatStr('yyyy-MM-dd')}}</p></div> //s ...

  9. phpMyAdmin历史漏洞复现

    简介 phpMyAdmin是一个非常受欢迎的基于web的MySQL数据库管理工具.它能够创建和删除数据库,创建/删除/修改表格,删除/编辑/新增字段,执行SQL脚本等 复现三个古老漏洞 phpMyAd ...

  10. Mybatis(四)多表操作

    数据库如下: 一.创建数据库所对应的bean类 public class User { private Integer uId; private String username; private St ...