码迷,mamicode.com
首页 > 其他好文 > 详细

890. Find and Replace Pattern - LeetCode

时间:2018-08-22 00:17:49      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:equal   cti   vat   for   ddn   举例   abc   problems   word   

Question

890. Find and Replace Pattern

技术分享图片

Solution

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

思路:

举例:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
abc -> 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

标签:equal   cti   vat   for   ddn   举例   abc   problems   word   

原文地址:https://www.cnblogs.com/okokabcd/p/9515192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!