Program to check the string is repeating string or not in Python


Suppose we have a string, we have to check whether it's a repeating string or not.

So, if the input is like string = "helloworldhelloworld", then the output will be True

To solve this, we will follow these steps −

  • n := size of s
  • Define a function findFactors() . This will take n
  • f := a new set
  • i := 1
  • while i * i <= n, do
    • if n mod i is same as 0, then
      • insert quotient of (n / i) into f
      • insert i into f
    • i := i + 1
  • return f
  • From the main method, do the following −
  • fact := findFactors(n)
  • for each i in fact, do
    • if i is same as n, then
      • go for the next iteration
    • ss := s[from index 0 to i-1]
    • val := occurrences of ss in s
    • if val is same as quotient of (n/i), then
      • return True
  • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      n = len(s)
      def findFactors(n):
         f = set()
         i = 1
         while(i * i <= n):
            if(n % i == 0):
               f.add(int(n / i))
               f.add(i)
            i+= 1
            return f
         fact = findFactors(n)
         for i in fact:
            if(i == n):
               continue
            ss = s[:i]
            val = s.count(ss)
            if(val == int(n / i)):
               return True
         return False
ob = Solution()
print(ob.solve("helloworldhelloworld"))

Input

"helloworldhelloworld"

Output

True

Updated on: 07-Oct-2020

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements