python regular expression solution

  • 2
    def repeatedSubstringPattern(self, str):
        :type str: str
        :rtype: bool
        import re
        return bool(re.match(r"^([a-z]+)\1+$", str))

  • 0

    This works as well:

    def repeatedSubstringPattern(self, str):
        return bool(re.match(r'(.+)\1+$', str))

    (^ isn't needed for re.match, [a-z] is an unnecessary restriction, and importing isn't needed here because LeetCode imports re (and many other) modules for us.)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.