Skip to content

Commit 639a087

Browse files
authored
Create 。。。.md
1 parent 9d8088a commit 639a087

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2018.11.30-leetcode890/。。。.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* words中的单词与pattern逐个字母对比,如果map中value为-1则保存映射关系,
3+
* 否则进行比较是否为同一映射关系,相同则继续比较,否则表明该字符不符合
4+
* */
5+
public List<String> findAndReplacePattern(String[] words, String pattern) {
6+
List<String> results = new ArrayList<>();
7+
Set characters = new HashSet<Character>();
8+
Map<Character,Integer> map = new HashMap<>();//map保存字符映射关系
9+
out:
10+
for (int i = 0; i < words.length; i++) {//遍历words
11+
for (int j = 0; j < pattern.length(); j++) {//初始化map
12+
map.put(pattern.charAt(j), -288);
13+
}
14+
for (int k = 0; k < words[i].length(); k++) {
15+
if (map.get(pattern.charAt(k)) == -288 && !characters.contains(words[i].charAt(k))) {
16+
map.put(pattern.charAt(k), pattern.charAt(k) - words[i].charAt(k));
17+
characters.add(words[i].charAt(k));
18+
} else {
19+
if (words[i].charAt(k) != -map.get(pattern.charAt(k)) + pattern.charAt(k)) {
20+
characters.clear();
21+
continue out;
22+
}
23+
}
24+
}
25+
results.add(words[i]);
26+
characters.clear();
27+
}
28+
return results;
29+
}

0 commit comments

Comments
 (0)