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

890. 查找和替换模式

时间:2018-08-19 15:49:27      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:weekly   void   ring   nbsp   abc   查找   substring   under   div   

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/weekly-contest-98/problems/find-and-replace-pattern/
//890. 查找和替换模式
public class Lesson890 {
    public static void main(String[] args) {
        String[] words = {"abc","deq","mee","aqq","dkd","ccc"};
        String pattern = "abb";
        List<String> res = findAndReplacePattern(words, pattern);
        System.out.println(res);
    }
    public static List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> list = new ArrayList<>(8);
        String ptn = getPattern(pattern);
        System.out.println(ptn);
        for(int i=0;i<words.length;i++) {
            String word = words[i];
            String pattern1 = getPattern(word);
            if (ptn.equals(pattern1)) {
                list.add(word);
            }
        }
        return list;
    }

    private static String getPattern(String pattern) {
        String ptn = "";
        Map<String, Integer> map = new HashMap<>(32);
        for(int i=0;i<pattern.length();i++) {
            String substring = pattern.substring(i, i + 1);
            Integer integer = map.get(substring);
            if (integer == null) {
                int size = map.size();
                map.put(substring, size + 1);
            }
            Integer integer_2 = map.get(substring);
            ptn = ptn+integer_2;
        }
        return ptn;
    }
}

 

890. 查找和替换模式

标签:weekly   void   ring   nbsp   abc   查找   substring   under   div   

原文地址:https://www.cnblogs.com/stono/p/9501191.html

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