Repeated Substring Pattern

Problem Id: 459 Difficulty: Easy


Intuition

Solution


class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        l = len(s)
        for i in range(1, l // 2 + 1):
            if not l / i == l // i:
                continue
            if s == s[:i] * (l // i):
                return True
        return False


# Test
import unittest


class TestSolution(unittest.TestCase):
    def test_case_1(self):
        self.assertTrue(
            Solution().repeatedSubstringPattern('abab')
        )

    def test_case_2(self):
        self.assertFalse(
            Solution().repeatedSubstringPattern('aba')
        )

    def test_case_3(self):
        self.assertTrue(
            Solution().repeatedSubstringPattern('abcabcabcabc')
        )


if __name__ == '__main__':
    unittest.main()