890. Find and Replace Pattern - LeetCode

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;
}

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

时间: 2024-10-31 13:56:55

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 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 d

[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 letter?x?in the pattern with?p(x), we get the d

890.&#160;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 d

Word Pattern - LeetCode

Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = "abba", str = "dog cat cat d

[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 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 d

arts-week6

Algorithm 830. Positions of Large Groups - LeetCode 75. Sort Colors - LeetCode 859. Buddy Strings - LeetCode 791. Custom Sort String - LeetCode 804. Unique Morse Code Words - LeetCode 877. Stone Game - LeetCode 890. Find and Replace Pattern - LeetCod

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

JavaScript——关于字符串的replace函数中的function函数的参数

1 <!DOCTYPE> 2 <html> 3 <head> 4 </head> 5 <body> 6 <script type="text/javascript"> 7 function test(s){ 8 var pattern=/&([^&;]+);/g; 9 s.replace(pattern,function(a,b,c,d,e){ 10 alert(a); 11 alert(b); 1

js正则替换replace

content = "<p><strong>发斯蒂芬斯</strong>蒂芬434324</p><p><strong>范德萨发</strong>的发斯蒂芬</p><p><strong>242<b>3423</b>423</strong></p>"; console.log(content); var pattern =