Skip to content

Commit 038b67f

Browse files
committed
문자열 패턴 찾기
1 parent 2d39e56 commit 038b67f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

WordPattern.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.HashMap;
2+
3+
// my solution_O(n)
4+
public class WordPattern {
5+
public boolean wordPattern(String pattern, String s) {
6+
HashMap<Character, String> hashMap = new HashMap<>();
7+
StringBuffer strPattern = new StringBuffer(pattern);
8+
String[] str = s.split(" ");
9+
10+
if(str.length != strPattern.length())
11+
return false;
12+
13+
for(int i = 0; i<str.length; i++){
14+
if(!hashMap.containsKey(strPattern.charAt(i))) {
15+
if(hashMap.containsValue(str[i]))
16+
return false;
17+
hashMap.put(strPattern.charAt(i), str[i]);
18+
}
19+
else {
20+
if(!hashMap.get(strPattern.charAt(i)).equals(str[i]))
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
}

0 commit comments

Comments
 (0)