Algorithm/릿코드
99클럽 코테 스터디 18일차 2942. Find Words Containing Character
Albosa2lol
2024. 6. 16. 11:39
https://leetcode.com/problems/find-words-containing-character/description/
문자 포함 단어 찾기 (Find Words Containing Character)
문제 설명:
문자열의 리스트 words와 단일 문자 ch가 주어졌을 때, ch를 포함하는 words 리스트의 모든 단어를 반환하세요. 반환하는 리스트의 순서는 입력 리스트에서의 순서와 동일해야 합니다.
제약 사항:
- 1 <= words.length <= 100
- 1 <= words[i].length <= 100
- ch는 하나의 영문자입니다.
- 모든 words[i]와 ch는 소문자로 이루어져 있습니다.
풀이
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
result = []
for i, word in enumerate(words):
if x in word:
result.append(i)
return result
1. 반복문 사용: words 리스트를 순회하면서 각 단어가 ch 문자를 포함하는지 확인
2. 포함 여부 체크: 각 단어가 ch 문자를 포함하면 결과 리스트에 추가