Sunday, March 20, 2022

Repeated Substring Python Codewars

Task:

For a given nonempty string s find a minimum substring t and the maximum number k, such that the entire string s is equal to t repeated k times.

The input string consists of lowercase latin letters.

Your function should return :

  • a tuple (t, k) (in Python)
  • an array [t, k] (in Ruby and JavaScript)
  • in C, return k and write to the string t passed in parameter

Example #1:

for string

s = "ababab";

the answer is

("ab", 3)

Example #2:

for string

s = "abcde"

the answer is

("abcde", 1)

because for this string "abcde" the minimum substring t, such that s is t repeated k times, is itself.

Solution:


def f(s):
    for i in range(1, len(s) + 1):
        t, k = s[:i], int(len(s) / i)
        if t * k == s:
            return (t, k)

print(f("efefef"))

Repeated Substring Solutions | Codewars



Related Posts


No comments :

Post a Comment