티스토리 뷰
Problem
Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
Example 2:
Input: words = ["a","b","c"], pattern = "a"
Output: ["a","b","c"]
Constraints:
- 1 <= pattern.length <= 20
- 1 <= words.length <= 50
- words[i].length == pattern.length
- pattern and words[i] are lowercase English letters.
Solution
단어 배열과 패턴이 주어지면 패턴과 일치하는 단어들을 반환하는 문제입니다.
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> list = new ArrayList<>();
for (String word : words) {
if (matches(word, pattern)) {
list.add(word);
}
}
return list;
}
private static boolean matches(String word, String pattern) {
for (int i = 0; i < word.length(); i++) {
if (word.indexOf(word.charAt(i)) != pattern.indexOf(pattern.charAt(i))) {
return false;
}
}
return true;
}
}
단어의 i번째 문자와 패턴의 i번째 문자에 해당하는 인덱스가 동일한 경우 단어가 해당 패턴을 가진다는 뜻이므로 모두 일치하는 단어들만 추가하여 반환하면 됩니다.
Test
package io.lcalmsky.leetcode.find_and_replace_pattern;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
void testAll() {
assertAll(
() -> test(new String[]{"abc", "deq", "mee", "aqq", "dkd", "ccc"}, "abb",
List.of("mee", "aqq")),
() -> test(new String[]{"a", "b", "c"}, "a", List.of("a", "b", "c"))
);
}
private void test(String[] words, String pattern, List<String> expected) {
// when
Solution solution = new Solution();
List<String> actual = solution.findAndReplacePattern(words, pattern);
// then
assertEquals(expected, actual);
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
377. Combination Sum IV (0) | 2022.08.06 |
---|---|
729. My Calendar I (0) | 2022.08.05 |
34. Find First and Last Position of Element in Sorted Array (0) | 2022.07.29 |
86. Partition List (0) | 2022.07.23 |
92. Reverse Linked List II (0) | 2022.07.22 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JSON
- spring boot jwt
- 스프링 부트 애플리케이션
- 함께 자라기 후기
- Linux
- r
- Spring Boot JPA
- proto3
- 클린 아키텍처
- Jackson
- 스프링 부트
- Java
- @ManyToOne
- intellij
- 스프링부트
- JPA
- 함께 자라기
- QueryDSL
- 스프링 부트 튜토리얼
- spring boot application
- 스프링 부트 회원 가입
- gRPC
- 스프링 데이터 jpa
- 헥사고날 아키텍처
- leetcode
- Spring Boot
- Spring Data JPA
- Spring Boot Tutorial
- spring boot app
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함