890. Find and Replace Pattern找出匹配形式的单词
[抄题]:
You have a list of words
and a pattern
, and you want to know which words in words
matches the pattern.
A word matches the pattern if there exists a permutation of letters p
so that after replacing every letter x
in the pattern with p(x)
, we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words
that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要像处理["abc","bcd","xyz"]一样,累计diff = string.charAt(i) - string.charAt(i - 1);
但是这道题其实和总偏移量没啥关系,abb aqq acc都可以。所以要用匹配
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
s[w.charAt(i)-'a']=p[pattern.charAt(i)-'a']=i+1;一次匹配一对,没匹配上不行
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
控制真假的boolean变量,在计算每个单词之前,要恢复成true的初始状态
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
s[0] = p[9] = 相同的位置坐标,一次匹配一对
[复杂度]:Time complexity: O(n2) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
//initialiazation
List<String> result = new ArrayList<String>(); //for loop for each word
for (String word : words) {
boolean match = true;
int[] w = new int[26]; int[] p = new int[26];
//for loop for each char
for (int i = 0; i < word.length(); i++) {
if (w[word.charAt(i) - 'a'] != p[pattern.charAt(i) - 'a']) {
match = false;
break;
} if (match == true) {
w[word.charAt(i) - 'a'] = i + 1;
p[pattern.charAt(i) - 'a'] = i + 1;
}
}
if (match == true) result.add(word);
} //return
return result;
}
}
890. Find and Replace Pattern找出匹配形式的单词的更多相关文章
- 890. Find and Replace Pattern - LeetCode
Question 890. Find and Replace Pattern Solution 题目大意:从字符串数组中找到类型匹配的如xyy,xxx 思路: 举例:words = ["ab ...
- 【LeetCode】890. Find and Replace Pattern 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+set 单字典 日期 题目地址:https:/ ...
- 使用c#正则验证关键字并找出匹配项
在.net里,使用类Regex可以正则验证一些关键字并取出匹配项. 1.使用Regex.IsMatch(string input, string pattern, RegexOptions ...
- LC 890. Find and Replace Pattern
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- [LeetCode] 890. Find and Replace Pattern 查找和替换模式
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- Leetcode 890. Find and Replace Pattern
把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...
- Sql Server 在数据库中所有表所有栏位 找出匹配某个值的脚本(转)
转自: http://blog.csdn.net/chenghaibing2008/article/details/11891419 (下面代码稍有修改,将要查找的内容直接作为参数传人,并且使用=而不 ...
- Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)
题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同.找到所有的子串位置,要求是words中字符串的一个连接: 举例: For example, given:s: &quo ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
随机推荐
- xamarin android 报错 Could not load assembly 'Xamarin.Android.Support.v7.AppCompat
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 Exception while loading assemblies: System.IO.FileNotFoundException: Cou ...
- js中常见事件
1.onblur:(使用在表单元素中,当元素失去焦点的时候执行) 2.onchange:(使用在表单元素中,当某些东西改变是执行) 3.onclick:(鼠标点击一个元素时执行) 4.ondblcli ...
- java多线程同步器
Java中多线程开发时,离不开线程的分工协作,常用的多线程的同步器有如下几种: 1.CountDownLatch 应用场景:等待一组线程任务完成后在继续执行当前线程. 用法:定义一个CountDown ...
- 定时重启tomcat
写个简单的定时重启,弄了一上午,主要是crontab里面奇怪 #!/bin/bash p=`ps -ef |grep tomcat |head -n 1|awk -F" " '{p ...
- [转]Linux下Python与C++混合编程
转自:http://www.cnblogs.com/tevic/p/3645197.html 最近在做一个CUDA的项目,记录下学习心得. 系统 Linux 3.11.0-19-generic #33 ...
- OpenStack上搭建Q版的公共环境准备(step1)
vmware14 centos7.5minimal版 controller1节点虚拟硬件配置: CPU:1颗2核 Memory:2G 硬盘:20G 网卡: VMnet1(仅主机模式):关闭DHCP,手 ...
- 阿里四不像Fourinone
阿里四不像——分布式核心技术框架 Fourinone https://blog.csdn.net/shero_zsmj/article/details/52277194
- mosquitto centos安装配置
周末弄wordpress的Mysql,一不小心把wordpress弄不好了,写了的好几遍文章也没有了,一怒之下,把整个系统重装了,安装了不带任何软件的新系统,重新搭一遍. 0.安装ftp服务器 #yu ...
- Ubuntu 16.04 安装 JDK 1.8
系统环境 Ubuntu 16.04; JDK 1.8 配置安装 1.首先从oracle下载jdk 1.8,我下载的版本是jdk-8u131-linux-x64.tar.gz,运行tar zvxf jd ...
- 廖雪峰Java7处理日期和时间-2Data和Calendar-1Date
计算机中如何存储和表示日期和时间 Epoch Time:从1970年1月1日零点(格林威治时区/GMT+00:00)到现在经历的秒数,也叫timestamp, 例如: 秒级: * 北京 2016-11 ...