Sangmun

459. Repeated Substring Pattern 본문

알고리즘/리트코드

459. Repeated Substring Pattern

상상2 2023. 8. 12. 09:38

https://leetcode.com/problems/repeated-substring-pattern/

 

Repeated Substring Pattern - LeetCode

Can you solve this real interview question? Repeated Substring Pattern - Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.   Example 1: Input: s = "abab" Output: true Expl

leetcode.com

 

문자열의 길이가 최대 10000까지 주어져서 일일이 substring을 나눠서 True/False를 판독하면 시간초과가 걸릴 줄 알았으나 그렇게 풀어도 통과과 됨

def repeatedSubstringPattern(s: str) -> bool:
    n = len(s)
    for i in range(1, n // 2 + 1):
        if n % i == 0:
            substring = s[:i]
            if substring * (n // i) == s:
                return True
    return False

'알고리즘 > 리트코드' 카테고리의 다른 글

283. Move Zeroes  (0) 2023.05.04
Palindrome  (0) 2023.04.27
516. Longest Palindromic Subsequence  (0) 2023.04.22
662. Maximum Width of Binary Tree  (0) 2023.04.20
Leet code Validate Stack Sequences  (0) 2023.04.13
Comments