728x90
리스트 내의 어떤 문자열이, 다른 문자열에 내포된 경우를 모아서 return하면 되는 문제이다.
class Solution(object):
def stringMatching(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
n = len(words)
wor = []
for a in range(n):
for b in range(n):
if a != b and words[b].find(words[a]) != -1:
wor.append(words[a])
break
return wor
- n으로 words 리스트의 갯수 확인
- 서로 다른 요소를 비교하기 위해서는, a와 b가 같아서는 안된다(ex. 똑같은 리스트 내에 1번과 1번을 비교하는 일)
- 그리고 words[b]에서 words[a]를 찾은 결과값이 -1(find 결과 없으면 -1을 산출한다)이 아니면, wor 리스트에 추가하고 break
Runtime : 6ms (30.22%)
Memory : 12.54MB (5.78%)
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Python] 2657. Find the Prefix Common Array of Two Arrays (0) | 2025.01.14 |
---|---|
[Python] 3223. Minimum Length of String After Operations (0) | 2025.01.14 |
[Python] 2116. Check if a Parentheses String Can Be Valid (0) | 2025.01.12 |
[Python] 876. Middle of the Linked List (0) | 2025.01.12 |
[Python] 1400. Construct K Palindrome Strings (0) | 2025.01.12 |