Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit242e8f6

Browse files
add a solution for 890
1 parent03aa4e5 commit242e8f6

File tree

1 file changed

+32
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+32
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_890.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,36 @@ public List<String> findAndReplacePattern(String[] words, String pattern) {
3535
returnresult;
3636
}
3737
}
38+
39+
publicstaticclassSolution2 {
40+
publicList<String>findAndReplacePattern(String[]words,Stringpattern) {
41+
List<String>result =newArrayList<>();
42+
for (Stringword :words) {
43+
if (matches(word,pattern)) {
44+
result.add(word);
45+
}
46+
}
47+
returnresult;
48+
}
49+
50+
privatebooleanmatches(Stringword,Stringpattern) {
51+
Map<Character,Character>map1 =newHashMap<>();//word -> p
52+
Map<Character,Character>map2 =newHashMap<>();//p -> word
53+
for (inti =0;i <pattern.length();i++) {
54+
if (!map1.containsKey(word.charAt(i))) {
55+
map1.put(word.charAt(i),pattern.charAt(i));
56+
}
57+
if (map1.containsKey(word.charAt(i)) &&map1.get(word.charAt(i)) !=pattern.charAt(i)) {
58+
returnfalse;
59+
}
60+
if (!map2.containsKey(pattern.charAt(i))) {
61+
map2.put(pattern.charAt(i),word.charAt(i));
62+
}
63+
if (map2.containsKey(pattern.charAt(i)) &&map2.get(pattern.charAt(i)) !=word.charAt(i)) {
64+
returnfalse;
65+
}
66+
}
67+
returntrue;
68+
}
69+
}
3870
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp