1

How would you go about splitting a normal string in to as many identical pieces as possible whilst using all characters. For example

a = "abab"

Would return "ab", whereas with

b= "ababc"

It would return "ababc", as it can't be split into identical pieces using all letters.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
A tsang
  • 21
  • 1
  • 4
  • 1
    Possible duplicate of [Count occurrence of a character in a string](http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – Obsidian Age Mar 26 '17 at 23:48
  • 1
    @ObsidianAge no, that's not a duplicate. This question is looking for repeating substrings, not a character count. – Zero Piraeus Mar 26 '17 at 23:51

2 Answers2

6

This is very similar, but not identical, to How can I tell if a string repeats itself in Python? – the difference being that that question only asks to determine whether a string is made up of identical repeating substrings, rather than what the repeating substring (if any) is.

The accepted (and by far the best performing) answer to that question can be adapted to return the repeating string if there is one:

def repeater(s):
    i = (s+s)[1:-1].find(s)
    if i == -1:
        return s
    else:
        return s[:i+1]

Examples:

>>> repeater('abab')
'ab'
>>> repeater('ababc')
'ababc'
>>> repeater('xyz' * 1000000)
'xyz'
>>> repeater('xyz' * 50 + 'q')
'xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzq'
Community
  • 1
  • 1
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
0

It seems that repeating substring has no pre and after letters, so it also could be this way:

In[4]: re.sub(r'^([a-z]+)\1$',r'\1','abab')
Out[4]: 'ab'
In[5]: re.sub(r'^([a-z]+)\1$',r'\1','ababc')
Out[5]: 'ababc' 

([a-z]+) means substring, \1 means repeat.

EDIT :

re.sub(r'^([a-z]+)\1{1,}$',r'\1','abcabcabcabc')
'abc'
Shenglin Chen
  • 4,504
  • 11
  • 11