890. Find and Replace Pattern - LeetCode
Question

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的更多相关文章
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+set 单字典 日期 题目地址:https:/ ...
- Leetcode 890. Find and Replace Pattern
把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...
- 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 ...
- [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 ...
- Repeated Substring Pattern Leetcode
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- Word Pattern - LeetCode
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- leetcode_两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同 ...
- ImportError: No module named 'Tkinter' [closed]
跑maskrcnn报错:UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot sh ...
- html5文件上传断点续传
最近公司要做一个html5上传的jquery插件,要在下先实现功能,要求显示上传进度,文件信息,断点续传等等.我一看,艾玛!Σ(゚д゚lll),没做过啊.没办法,(# ゚Д゚),只能去查资料了.作为一 ...
- 界面跳转+Android Studio Button事件的三种方式
今天学习界面跳转 java类总是不能新建成功 看了网上教程 (20条消息) 关于android studio无法创建类或者接口问题的解决方法_qq_39916160的博客-CSDN博客 可以新建了 但 ...
- CCF201312-2ISBN号码
问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如"x-xxx-xxxxx-x",其中符号"-&qu ...
- Spring理解1 ioc
Spring Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器(框架). 需要了解 ioc容器 IOC底层原理 IOC接口 BeanFactory Bean的作用域 IOC操 ...
- ubuntu修复找不到sudo命令
1.首先,您需要安装该sudo命令.你可以使用 apt 包管理器来做到这一点.您需要以有权安装软件包的用户身份运行此命令,例如root: apt-get install sudo 2.下一步是为您自己 ...
- docker更新portainer-ce2.0
前两天,我在使用portainer的过程中发现左下角提醒有新版本的portainer需要安装,google了一圈如何升级portainer,并没有找到我需要的资料,就算获取了portainer:las ...
- Mybatis注解开发(一对一)
其他代码访问:Mybatis注解开发基础操作 1.添加OrderMapper接口 public interface OrderMapper { // @Select("select *,o. ...
- 将border 边框换成图片 border-image
<template> <div class="heart"></div> </template> <script> ...