- Date : 2021.09.26(일)
- Time : 25분
- Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
- 1 <= s.length <= 10^4
- s consists of lowercase English letters.
- Input: s = "abcabcabcabc"
- Output: true
- Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
def repeatedSubstringPattern(self, s: str) -> bool:
answer = []
for i in range(0, len(s)):
if s[0:i + 1] * (len(s) // (i + 1)) == s:
answer.append([s[0:i + 1], (len(s) // (i + 1))])
return (len(answer) > 1)
: 이 문제는 단어가 특정한 패턴의 반복
으로 구성되어있는가를 묻는 문제이다. 그래서 for 문을 사용해서 처음부터 끝까지 돌았다. 이 문제에서 만약 패턴이 존재한다면 answer이라는 배열안에 최소한 2개의 값이 있어야한다. 그 2개는 바로 패턴값과 단어
이다. if문을 살펴보면 현재 for문에서 주어지는 i 값으로 단어를 슬라이싱 한다. 그리고 그 슬라이싱 한 단어를 나머지 갯수만큼 다시 붙였을 때 원래의 단어와 동일하다면 현재 슬라이싱 된 단어가 패턴이라는 의미가 된다. 그리고 왜 1개이면 안되냐를 묻는다면 맨 마지막 최종 단어가 무조건 answer에 삽입되기 때문에 1개 이상을 검사해야한다.