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

Commitbf94760

Browse files
word pattern
1 parentac85f2d commitbf94760

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

‎EASY/src/easy/WordPattern.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packageeasy;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclassWordPattern {
7+
8+
publicbooleanwordPattern(Stringpattern,Stringstr) {
9+
String[]words =str.split(" ");
10+
char[]patterns =pattern.toCharArray();
11+
Map<Character,String>map =newHashMap();
12+
if(patterns.length !=words.length)returnfalse;
13+
for(inti =0;i <patterns.length;i++){
14+
if(map.containsKey(patterns[i])){
15+
if(!map.get(patterns[i]).equals(words[i]))returnfalse;
16+
}else {
17+
if(map.containsValue(words[i]))returnfalse;//this is for this case: "abba", "dog dog dog dog"
18+
map.put(patterns[i],words[i]);
19+
}
20+
}
21+
returntrue;
22+
}
23+
24+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution]| ? | ? | Hard| BFS
2424
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../../blob/master/EASY/src/easy/BullsandCows.java)| O(n)|O(1)| Easy|
2525
|292|[Nim Game](https://leetcode.com/problems/nim-game/)|[Solution](../../blob/master/EASY/src/easy/NimGame.java)| O(1)|O(1)| Easy|
26+
|290|[Word Pattern](https://leetcode.com/problems/word-pattern/)|[Solution](../../blob/master/EASY/src/easy/WordPattern.java)| O(n)|O(n)| Easy|
2627
|289|[Game of Life](https://leetcode.com/problems/game-of-life/)|[Solution](../../blob/master/MEDIUM/src/medium/GameOfLife.java)| O(m*n)|O(m*n), could be optimized to O(1)| Medium|
2728
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../../blob/master/EASY/src/easy/MoveZeroes.java)| O(n)|O(1)| Easy|
2829
|280|[Wiggle Sort](https://leetcode.com/problems/wiggle-sort/)|[Solution](../../blob/master/MEDIUM/src/medium/WiggleSort.java)| O(n)|O(1)| Medium|

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp