问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. OSCP Learning Notes - Overview

    Prerequisites: Knowledge of scripting languages(Bash/Pyhon) Understanding of basic networking concep ...

  2. Ethical Hacking - NETWORK PENETRATION TESTING(7)

    Gaining Access to encrypted networks Three main encryption types: 1. WEP 2.WPA 3.WPA2 WEP Cracking W ...

  3. 面试官:连Spring三级缓存都答不好,自己走还是我送你?

    面试官:简历上写了精通Spring,那你回答一下Spring为什么用“三级缓存”去解决循环依赖? 我:.......应该有三个缓存的map结构 面试官:具体回答一下 我:平时没认真深入过 面试官:公司 ...

  4. three.js 数学方法之Box3

    从今天开始郭先生就会说一下three.js 的一些数学方法了,像Box3.Plane.Vector3.Matrix3.Matrix4当然还有欧拉角和四元数.今天说一说three.js的Box3方法(B ...

  5. org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication

    Ftp问题 最近遇到了ftp读取中文乱码的问题,代码中使用的是FtpClient.google一下找到了解决方案. FTP协议里面,规定文件名编码为iso-8859-1,FTP类中默认的编码也是这个. ...

  6. 第【7】章: 数递归、DFS、剪枝、回溯等问题 学习报告

    (7.2)节:   递归问题    1.题干: 递归方便表达,但是性能上消耗过多 1.有个小孩正在上楼梯,楼梯有n阶台阶,小孩一次可以上1阶.2阶.3阶. 请实现一个方法,计算小孩有多少种上楼的方式. ...

  7. Shell变量的作用域:Shell全局变量、环境变量和局部变量

    Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...

  8. Bug--Tomcat Error start child

    添加Quartz之后报错 下面的Cause by: More than one fragment with the name [spring_web] was found. This is not l ...

  9. Java Servlet详解(体系结构+注解配置+生命周期)

    Java Servlet详解(注解配置+生命周期) 什么是Servlet : (Server applet)? 顾名思义:服务端的小程序 Servlet只是一个接口,定义了Java被浏览器访问到(To ...

  10. 社交网站的数据挖掘与分析pdf版本|网盘下载地址附提取码|

    点击此处进入网盘下载地址 提取码:btqx 作者介绍: 马修·罗塞尔(MatthewA.Russell),DigitalReasoningSystems公司的技术副总裁和Zaffra公司的负责人,是热 ...