Question

890. Find and Replace Pattern

Solution

题目大意:从字符串数组中找到类型匹配的如xyy,xxx

思路:

举例:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
abb -> 011 abc -> 012
deq -> 012
mee -> 011 匹配
aqq -> 011 匹配
dkd -> 010
ccc -> 000

Java实现:

public List<String> findAndReplacePattern(String[] words, String pattern) {
int[] patternPos = getStrPos(pattern);
List<String> retList = new ArrayList<>();
for (String word : words) {
if (Arrays.equals(getStrPos(word), patternPos)) retList.add(word);
}
return retList;
} private int[] getStrPos(String str) {
Map<Character, Integer> posMap = new HashMap<>();
char[] arr = str.toCharArray();
int[] posArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
if (posMap.get(arr[i]) == null) {
posMap.put(arr[i], i);
}
posArr[i] = posMap.get(arr[i]);
}
return posArr;
}

890. Find and Replace Pattern - LeetCode的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】890. Find and Replace Pattern 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+set 单字典 日期 题目地址:https:/ ...

  4. Leetcode 890. Find and Replace Pattern

    把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...

  5. 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 ...

  6. [Swift]LeetCode890. 查找和替换模式 | 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 ...

  7. Repeated Substring Pattern Leetcode

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  8. Word Pattern - LeetCode

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. 【C语言】预处理、宏定义、内联函数 _

    一.由源码到可执行程序的过程 1. 预处理: 源码经过预处理器的预处理变成预处理过的.i中间文件   1 gcc -E test.c -o test.i 2. 编译: 中间文件经过编译器编译形成.s的 ...

  2. ImportError: No module named 'Tkinter' [closed]

    跑maskrcnn报错:UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot sh ...

  3. Hive进行数据统计时报错:org.apache.hadoop.mapreduce.v2.app.MRAppMaster: Error starting MRAppMaster

    报错详情: 2020-04-09 22:56:58,827 ERROR [Listener at 0.0.0.0/45871] org.apache.hadoop.mapreduce.v2.app.M ...

  4. vuejs兄弟组件之间的通信

    var Event = new Vue();//准备一个空的实例对象 //A组件 var A = { template: ` <div> <span>我是A组件的数据-> ...

  5. Spring理解1 ioc

    Spring Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器(框架).   需要了解 ioc容器 IOC底层原理 IOC接口 BeanFactory Bean的作用域 IOC操 ...

  6. phpshe xml注入

    *php商城系统 xml注入* **页面样式* *Xml原理参考:* https://www.cnblogs.com/20175211lyz/p/11413335.html *漏洞函数simplexm ...

  7. MySQL---char和varchar的区别

    char和varchar的区别 char表示定长, 即长度固定. varchar表示变长, 即长度可变. 当输入数据的长度小于定义的长度时, char会用空格填充, 而varchar则按照实际长度存储 ...

  8. 企业需要使用网络损伤仪 WANsim 的帮助,以便更高效地迁移到云端

    正确解决与云环境中的应用程序部署有关的问题需要针对每个系统的独特需求以寻找特定的网络工具.网络损伤仪 WANsim 助力企业更高效地迁移到云端! 起初,云厂商以在云端办公相对于传统方式拥有更高的可靠性 ...

  9. Servlet 标准下载地址

    Servlet 标准下载地址 java 技术标准开发社区 https://jcp.org/ 3.0 https://jcp.org/aboutJava/communityprocess/mrel/js ...

  10. RecyclerView + SQLite 简易备忘录-----中(2)

    (3)RecyclerView的实现 ---中间的内容 RecyclerView是一个比ListView更加强大的滚动控件.要使用这个控件需要先在项目的build.gradle中添加RecyclerV ...