leetcode890 Java实现

news/2025/3/20 7:49:29/

leetcode890 Java实现

  • 题目
    在这里插入图片描述

  • 思路
    想办法找个每个字符相对应的映射关系,在这里我把每个字符都映射成不同的数字,每个字符都对应一个数字,若目标字符串的字符对应的数字与输入字符串数组中的字符对映数字相等,则该字符串符合要求。

  • Code

class Solution {public List<String> findAndReplacePattern(String[] words, String pattern) {HashMap<Character, Integer> set1 = new HashMap<>();HashMap<Character, Integer> set2 = new HashMap<>();List<String> l = new ArrayList<>();for(int i=0;i<pattern.length();i++) {set1.put(pattern.charAt(i), i);}for(int i=0;i<words.length;i++) {for(int j=0;j<pattern.length();j++) {set2.put(words[i].charAt(j), j);}if(dui(set2,set1,words[i],pattern)) {l.add(words[i]);}}return l;}public static boolean dui(HashMap<Character, Integer> m,HashMap<Character, Integer> n,String s1,String s2) {boolean flag =true;for(int z=0;z<s2.length();z++) {if(m.get(s1.charAt(z))!=n.get(s2.charAt(z))) {flag = false;break;}}return flag;}
}

http://www.ppmy.cn/news/542398.html

相关文章

LeetCode: 890. Find and Replace Pattern

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 l…

Acwing 890. 能被整除的数

Acwing 890. 能被整除的数 题目描述 给定一个整数 n 和 m 个不同的质数 p1,p2,…,pm。 请你求出 1∼n中能被 p1,p2,…,pm 中的至少一个数整除的整数有多少个。 输入格式 第一行包含整数 n 和 m。 第二行包含 m个质数。 输出格式 输出总共的个数。数据范围 1≤m≤16, 1…

LeetCode Problems #890

2018年9月16日 #890. Find and Replace Pattern 问题描述&#xff1a; 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 …

890. Find and Replace Pattern

# 简单题&#xff0c;直接对输入做一个变换就可以 class Solution { public:string change_word_format(string word){string result "";map<char, string> s;int index 0;for(int i0;i<word.length();i){if(s.find(word[i])s.end()){s[word[i]]to_strin…

890. 查找和替换模式

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/we…

tcl电视linux系统怎么升级,TCL电视MS80104系列最新890版升级方法及效果图!

原标题:TCL电视MS80104系列最新890版升级方法及效果图! MS80104系列系统UI全面升级890版 件版本号:V8-MS80104-LF1V890芯片方案:MS801 ()固件适用机型: TCL智能电视F3390/E5300/E5050/E5060/E4500/E4380/E5390/F3500/E5500系列 亲测好用,稳定,无bug,有需要的可以自行下载,…

2022河海大学物联网工程学院电子信息(计算机与软件方向)890上岸经验帖(毕业2年后,双非三跨211成功)

需要真题资料或者辅导的加扣扣2389886090 一.个人情况介绍 19年毕业于江苏末流二本的化学工程与工艺专业&#xff0c;随后转行为java后端开发工程师&#xff08;南京的某手机厂商&#xff0c;人力外包进去的&#xff09;&#xff0c;21年7月左右辞职&#xff0c;全力冲刺考研…

php1 890,PHP正则表达式:将1234567890转换成1,234,567,890 每3位用逗号隔开的形式

“四项处理”的作用是与字符相匹配&#xff0c;然后根据是否能找到要匹配的字符&#xff0c;从而确定下来是否有符合要匹配样式的位置&#xff0c;在这个过程中&#xff0c;“四项处理”的匹配操作并不会占用目标字符串&#xff0c;它只是纯粹用于位置判断。下面是“四项处理”…