Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding if a string is an iterative substring Algorithm in C?

Problem Description

I have a string S. How can I find if the string follows S = nT ?

Examples:

Function should return true if

1) S = "abab"  
2) S = "abcdabcd"  
3) S = "abcabcabc"  
4) S = "zzxzzxzzx"  

But if S="abcb" returns false.

I though maybe we can repeatedly call KMP on substrings of S and then decide.

i.e.:

for "abab": call on KMP on "a". it returns 2(two instances). now 2*len("a")!=len(s)

call on KMP on "ab". it returns 2. now 2*len("ab")==len(s) so return true

Can you suggest any better algorithms?

like image 260
EsotericMe Avatar asked Jan 14 '11 22:01

EsotericMe


1 Answers

I can think of a heuristic, only call KMP on a sub string if Len(original string)/Len of(sub string) is a positive integer.

Also the maximum length of the sub string has to be less than N/2.

EDIT

Using these Heuristics Iwrote the follwing python code because my C is rusty at the moment

oldstr='ABCDABCD'    

for i in xrange(0,len(oldstr)/2):
       newslice=oldstr[0:i+1]
         if newslice*(len(oldstr)/len(newslice)) == oldstr:
             print 'pattern found', newslice
             break
like image 55
anijhaw Avatar answered Oct 14 '22 17:10

anijhaw