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 ...
随机推荐
- Go Example--defer
package main import ( "fmt" "os" ) func main() { f := createFile("/tmp/defe ...
- Python开发 標準內建方法 (未完代補)
abs(number) 絕對值 The abs() method takes a single argument: num - number whose absolute value is to ...
- Python Json & Pickle模块
用于序列化的两个模块 Json,用于字符串 和 python数据类型间进行转换 Pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...
- 使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)
使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development.production) --mode 指定环境模式 ...
- 【python】参数中的*args和**kwargs
转自https://www.cnblogs.com/xuyuanyuan123/p/6674645.html#undefined 多个实参,放到一个元组里面,以*开头,可以传多个参数:**是形参中按照 ...
- HTTP Protocol - URI
Uniform Resource Identifier (URI): compact sequence of characters that identifies an abstract or phy ...
- Oracle 语句整理
1 查出列当中重复的值 select ip2,count(*) from vm_info group by ip2 having count(*)>1 期中ip2为列名 vm_inf ...
- 基于keras的fasttext短文本分类
### train_model.py ### #!/usr/bin/env python # coding=utf-8 import codecs import simplejson as json ...
- Java产生死锁的一个简单例子
思路是创建两个字符串a和b,再创建两个线程A和B,让每个线程都用synchronized锁住字符串(A先锁a,再去锁b:B先锁b,再锁a),如果A锁住a,B锁住b,A就没办法锁住b,B也没办法锁住a, ...
- ORACLE重装之后恢复数据库,相当于sqlserver的附加数据库
在开发机器上经常会遇到重装系统的问题,重装之前如果ORACLE没有及时备份的话重装之后就纠结了,数据还原很头疼. 各种娘中只能找到一些ORACLE安装与重装系统前目录相同的解决办法,目录不同就没招了. ...