890.查找和替换模式

news/2025/3/20 7:49:29/

890.查找和替换模式

你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配。

如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我们就得到了所需的单词,那么单词与模式是匹配的。

(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)

返回 words 中与给定模式匹配的单词列表。

你可以按任何顺序返回答案。

示例:

输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
输出:["mee","aqq"]
解释:
"mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。
"ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。
因为 a 和 b 映射到同一个字母。

提示:

1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20

思路

先把pattern转成统一形式,这里用函数transfor设定了一个同一形式:当出现非重复项时,其值为当前索引值,若为重复项的时候,其值为第一次出现该项的索引值;后面只要将words中的字符串一个个转为统一形式再对比就可以了。

class Solution:def findAndReplacePattern(self, words, pattern):""":type words: List[str]:type pattern: str:rtype: List[str]"""res = []p0 = self.transfor(pattern)for item in words:if self.transfor(item) == p0:res.append(item)return resdef transfor(self,word):res = ""for index,item in enumerate(word):if res == "":res += str(index)elif word[0:index].find(item) != -1:res += str(word[0:index].find(item))else:res += str(index)return res

http://www.ppmy.cn/news/542399.html

相关文章

leetcode890 Java实现

leetcode890 Java实现 题目 思路 想办法找个每个字符相对应的映射关系&#xff0c;在这里我把每个字符都映射成不同的数字&#xff0c;每个字符都对应一个数字&#xff0c;若目标字符串的字符对应的数字与输入字符串数组中的字符对映数字相等&#xff0c;则该字符串符合要求。…

LeetCode: 890. Find and Replace Pattern

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 pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every l…

Acwing 890. 能被整除的数

Acwing 890. 能被整除的数 题目描述 给定一个整数 n 和 m 个不同的质数 p1,p2,…,pm。 请你求出 1∼n中能被 p1,p2,…,pm 中的至少一个数整除的整数有多少个。 输入格式 第一行包含整数 n 和 m。 第二行包含 m个质数。 输出格式 输出总共的个数。数据范围 1≤m≤16, 1…

LeetCode Problems #890

2018年9月16日 #890. Find and Replace Pattern 问题描述&#xff1a; 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 …

890. Find and Replace Pattern

# 简单题&#xff0c;直接对输入做一个变换就可以 class Solution { public:string change_word_format(string word){string result "";map<char, string> s;int index 0;for(int i0;i<word.length();i){if(s.find(word[i])s.end()){s[word[i]]to_strin…

890. 查找和替换模式

890. 查找和替换模式 https://leetcode-cn.com/contest/weekly-contest-98/problems/find-and-replace-pattern/ package com.test;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;//https://leetcode-cn.com/contest/we…

tcl电视linux系统怎么升级,TCL电视MS80104系列最新890版升级方法及效果图!

原标题:TCL电视MS80104系列最新890版升级方法及效果图! MS80104系列系统UI全面升级890版 件版本号:V8-MS80104-LF1V890芯片方案:MS801 ()固件适用机型: TCL智能电视F3390/E5300/E5050/E5060/E4500/E4380/E5390/F3500/E5500系列 亲测好用,稳定,无bug,有需要的可以自行下载,…

2022河海大学物联网工程学院电子信息(计算机与软件方向)890上岸经验帖(毕业2年后,双非三跨211成功)

需要真题资料或者辅导的加扣扣2389886090 一.个人情况介绍 19年毕业于江苏末流二本的化学工程与工艺专业&#xff0c;随后转行为java后端开发工程师&#xff08;南京的某手机厂商&#xff0c;人力外包进去的&#xff09;&#xff0c;21年7月左右辞职&#xff0c;全力冲刺考研…