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 ...
随机推荐
- UltraISO 9.7.1.3519注册码
王涛 7C81-1689-4046-626F redcaps 82C6-3DEF-AB07-0EC0
- iuplua test on luaforwindows
SW https://github.com/rjpcomputing/luaforwindows/releases Steps Install lua for windows write a bat ...
- Docker之 数据持久化
容器中数据持久化主要有两种方式: 数据卷(Data Volumes) 数据卷容器(Data Volumes Dontainers) 数据卷 数据卷是一个可供一个或多个容器使用的特殊目录,可以绕过UFS ...
- 剑指offer 1.数组 二维数组中查找
题目描述 在一个二维数组中(每个一维数组的长度相同), 每一行都按照从左到右递增的顺序排序, 每一列都按照从上到下递增的顺序排序. 请完成一个函数, 输入这样的一个二维数组和一个整数,判断数组中是否含 ...
- c#读sql server数据添加到MySQL数据库
using System;using System.Collections.Generic;using System.Text;using Console = System.Console;using ...
- AIOps指导
AIOps代表运维操作的人工智能(Artificial Intelligence for IT Operations), 是由Gartner定义的新类别,Gartner的报告宣称,到2020年, ...
- 关于bit,bin文件的一些研究
关于bit,bin文件的一些研究 bit文件里面有head information 但bin文件里面并不包含 bit 文件里面包含如下信息 SPI flash 时钟需要用到的源语 watchdog 设 ...
- 将SQL for xml path('')中转义的字符正常显示
在工作中出现的发送邮件的时候:因为邮件内容中有链接,并且多个拼接在一起的,于是用了for xml path(). 但是,这样显示出来的链接时会将路径中的<,>,&符号转 ...
- 【C++】vector内存机制和性能分析
转自:https://blog.csdn.net/mfcing/article/details/8746256 一些好的公司校园招聘过程中(包括笔试.面试环节),经常会涉及到STL中vector的使用 ...
- oracle表被锁(delete或update一直处于执行状态)的处理办法。
--首先查看有哪些锁 select /*+ rule */ s.username, decode(l.type,'TM','TABLE LOCK','TX','ROW LOCK',null) lock ...