| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 전이
- firebase
- 코틀린
- 수술
- 림프절전이
- 동위원소치료
- 방사성 동위원소 치료
- 알고리즘
- 임파선전이
- 카페
- 방사성동위원소치료
- Compose
- 맛집
- kotlin
- 프로그래머스
- 갑상선암
- android auto
- Android
- Android Compose
- 객체
- 안드로이드
- leetcode
- 입원
- 폐CT
- 정렬 알고리즘
- 백준알고리즘
- 자바
- MYSQL
- 저요오드식
- 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 |