[LeetCode] String Matching in an Array

아르당·7일 전

LeetCode

목록 보기
295/303
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

문자열 배열 words가 주어졌을 때, 다른 단어의 부분 문자열인 words에서 모든 문자열을 반환해라.

Example

#1
Input: words = ["mass", "as", "hero", "superhero"]
Output: ["as", "hero"]
Explanation: "as"는 "mass"의 부분 문자열이고 "hero"는 "superhero"의 부분 문자열이다.

#2
Input: words = ["leetcode", "et", "code"]
Output: ["et", "code"]
Explanation: "et", "code"는 "leetcode"의 부분 문자열이다.

#3
Input: words = ["blue", "green", "bu"]
Output: []
Explanation: 다른 문자열의 부분 문자열이 없다.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 30
  • words[i]는 영어 소문자로 구성된다.
  • words의 모든 문자열은 고유하다.

Solved

class Solution {
    public List<String> stringMatching(String[] words) {
        int l = words.length;
        List<String> result = new ArrayList<>();

        for(int i = 0; i < l; i++){
            for(int j = 0; j < l; j++){
                if(i != j && words[j].contains(words[i])){
                    result.add(words[i]);
                    break;
                }
            }
        }

        return result;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글