""" 0459.1 - Solution 1 - String Concatenation Trick """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def repeatedSubstringPattern(self, s: str) -> bool:
"""Repeated Substring Pattern Function"""
return s in (s + s)[1:-1]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().repeatedSubstringPattern("abab")) # True
print(Solution().repeatedSubstringPattern("aba")) # False
print(Solution().repeatedSubstringPattern("abcabcabcabc")) # True
print(Solution().repeatedSubstringPattern("a")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0459.2 - Solution 2 - Brute Force Substring Check """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def repeatedSubstringPattern(self, s: str) -> bool:
"""Repeated Substring Pattern Function"""
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
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().repeatedSubstringPattern("abab")) # True
print(Solution().repeatedSubstringPattern("aba")) # False
print(Solution().repeatedSubstringPattern("abcabcabcabc")) # True
print(Solution().repeatedSubstringPattern("a")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()