Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:47:11+00:00 2026-05-27T18:47:11+00:00

I am looking for an efficient way to extract the shortest repeating substring. For

  • 0

I am looking for an efficient way to extract the shortest repeating substring.
For example:

input1 = 'dabcdbcdbcdd'
ouput1 = 'bcd'

input2 = 'cbabababac'
output2 = 'ba'

I would appreciate any answer or information related to the problem.

Also, in this post, people suggest that we can use the regular expression like

re=^(.*?)\1+$

to find the smallest repeating pattern in the string. But such expression does not work in Python and always return me a non-match (I am new to Python and perhaps I miss something?).

— follow up —

Here the criterion is to look for shortest non-overlap pattern whose length is greater than one and has the longest overall length.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:47:11+00:00Added an answer on May 27, 2026 at 6:47 pm

    A quick fix for this pattern could be

    (.+?)\1+
    

    Your regex failed because it anchored the repeating string to the start and end of the line, only allowing strings like abcabcabc but not xabcabcabcx. Also, the minimum length of the repeated string should be 1, not 0 (or any string would match), therefore .+? instead of .*?.

    In Python:

    >>> import re
    >>> r = re.compile(r"(.+?)\1+")
    >>> r.findall("cbabababac")
    ['ba']
    >>> r.findall("dabcdbcdbcdd")
    ['bcd']
    

    But be aware that this regex will only find non-overlapping repeating matches, so in the last example, the solution d will not be found although that is the shortest repeating string. Or see this example: here it can’t find abcd because the abc part of the first abcd has been used up in the first match):

    >>> r.findall("abcabcdabcd")
    ['abc']
    

    Also, it may return several matches, so you’d need to find the shortest one in a second step:

    >>> r.findall("abcdabcdabcabc")
    ['abcd', 'abc']
    

    Better solution:

    To allow the engine to also find overlapping matches, use

    (.+?)(?=\1)
    

    This will find some strings twice or more, if they are repeated enough times, but it will certainly find all possible repeating substrings:

    >>> r = re.compile(r"(.+?)(?=\1)")
    >>> r.findall("dabcdbcdbcdd")
    ['bcd', 'bcd', 'd']
    

    Therefore, you should sort the results by length and return the shortest one:

    >>> min(r.findall("dabcdbcdbcdd") or [""], key=len)
    'd'
    

    The or [""] (thanks to J. F. Sebastian!) ensures that no ValueError is triggered if there’s no match at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for an efficient way to pull the data I want out
I'm looking for an efficient way to get the list of unique commit authors
I am looking for a most efficient way to re-use widgets with different selectors...
I am looking for an efficient way to calculate the traffic speed for my
I'm looking for an efficient way to implement the same kind of layout that
I'm looking for an efficient way to calculate the rank vector of a list
I'm looking for an efficient way to filter a specific color from a bitmapData
I'm looking for an efficient way to achieve this: you have a list of
I'm looking for an efficient way to implement a serialization mechanism in C. I
Given two floating-point numbers, I'm looking for an efficient way to check if they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.