일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 맛집
- 용인세브란스병원
- 림프절전이
- 임파선전이
- 프로그래머스
- 정렬 알고리즘
- 월간 코드 챌린지 시즌1
- Android
- 갑상선암용인세브란스
- leetcode
- 저요오드식
- 경력 개발자
- 동위원소치료
- 카페
- firebase
- 안드로이드
- 수술
- MYSQL
- 백준알고리즘
- 객체
- 폐CT
- 방사성동위원소치료
- 입원준비
- 갑상선암
- 전이
- 이직 준비
- java
- 방사성 동위원소 치료
- 입원
- 알고리즘
Archives
- Today
- Total
새우버거의 개발 블로그
[Java] LeetCode 290 : Word Pattern 본문
LeetCode # 290
Example 1:
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Note:
- 1 <= pattern.length <= 300
- pattern contains only lower-case English letters.
- 1 <= s.length <= 3000
- s contains only lower-case English letters and spaces ' '.
- s does not contain any leading or trailing spaces.
- All the words in s are separated by a single space.
Solution #1 (1ms)
class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character, String> map_char = new HashMap<>(); // <a, dog>, <b, cat>, <c, fish>
Map<String, Character> map_str = new HashMap<>(); // <dog, a>, <cat, b>, <fish, c>
String[] array = s.split(" ");
if (pattern.length() != array.length) return false;
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
String str = array[i];
if (map_char.get(c) == null) { // Map에 'a', 'b'. 'c'.. 의 값이 없을 경우
if (map_str.containsKey(str)) {
return false;
} else {
map_char.put(c, str);
map_str.put(str, c);
}
} else {
if (!map_char.get(c).equals(str)) {
return false;
}
}
}
return true;
}
}
Result #1
More Algorithm!
👇👇
github.com/ggujangi/ggu.leet-code
ggujangi/ggu.leet-code
LeetCode, Java. Contribute to ggujangi/ggu.leet-code development by creating an account on GitHub.
github.com
출처 : leetCode
'알고리즘 > LeetCode' 카테고리의 다른 글
[Java] LeetCode 874 : Walking Robot Simulation (0) | 2021.04.17 |
---|---|
[Java] LeetCode 1518 : Water Bottles (0) | 2021.04.17 |
[Java] LeetCode 914 : X of a Kind in a Deck of Cards (0) | 2021.04.17 |
[Java] LeetCode 1486 : XOR Operation in an Array (0) | 2021.04.17 |
[Java] LeetCode 977 : Squares of a Sorted Array (0) | 2021.03.30 |