# \[QUIZ\] Longest Repeated Substring (#153)

**URL:** <https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941>\
**Category:** ruby-talk\
**Created:** [21 January 2008 22:37 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941 "2008-01-21T22:37:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Koppel1](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@James\_Koppel1](https://rubytalk.org/u/James_Koppel1)\
**Post date:** [21 January 2008 22:37 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941/1 "2008-01-21T22:37:07Z")

</div>

My solution finds substrings of length n, groups the identical ones together in a hash of their starting indices, prunes out cases where two of the "repeated" substring overlap each other, and then looks in the same places for substrings of length n+1.

The aggressive pruning does, however, cause it to fail in the case where a substring that overlaps at one length does not overlap at a higher length. For example, take the string "aaaaaa". The substring "aa" exists at indicies [0,1,2,3,4]. My program will prune that down to [0,2,4]. It will then record the substring of length 3 that starts at each of [0,2,4], prune out the ones that start at 2 (overlaps with the one startign at 0) and 4 (goes out of bounds), leaving only an unrepeated substring, and would thus fall back and print "aa," even though the correct answer is "aaa," because the index of 3 had already been pruned.

On the other hand, the aggressive pruning does make my program somewhat fast. It takes about 45s on Also Sprach Zarathustra.

text = ARGF.read

substr\_locs = {}

text.split(//).each\_with\_index do |c,idx|  
&nbsp;&nbsp;if substr\_locs[c]  
&nbsp;&nbsp;&nbsp;&nbsp;substr\_locs[c] \<\< idx  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;substr\_locs[c] = [idx]  
&nbsp;&nbsp;end  
end

substr\_locs.each\_pair do |substr,locs|  
&nbsp;&nbsp;substr\_locs.delete(substr) if locs.length == 1  
end

len = 1  
done = false  
until done  
&nbsp;&nbsp;len += 1  
&nbsp;&nbsp;new\_substr\_locs = {}  
&nbsp;&nbsp;substr\_locs.each\_pair do |substr,locs|  
&nbsp;&nbsp;&nbsp;&nbsp;locs.each do |idx|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = text[idx,len]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if idx+len\>text.length  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if new\_substr\_locs[s]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_substr\_locs[s] \<\< idx  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_substr\_locs[s] = [idx]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;##Must reduce for the case where a substring overlaps itself  
&nbsp;&nbsp;new\_substr\_locs.each\_key do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;locs = new\_substr\_locs[s]  
&nbsp;&nbsp;&nbsp;&nbsp;idx = 1  
&nbsp;&nbsp;&nbsp;&nbsp;while idx \< locs.length  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if locs[idx]-locs[idx-1] \< len  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;locs.delete\_at(idx)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;idx += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;new\_substr\_locs.delete(s) if locs.length == 1  
&nbsp;&nbsp;end  
&nbsp;&nbsp;unless new\_substr\_locs.keys.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;substr\_locs = new\_substr\_locs  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;done = true  
&nbsp;&nbsp;end  
end  
puts substr\_locs.keys.first

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Looking for last minute shopping deals?  
> Find them fast with Yahoo! Search. [http://tools.search.yahoo.com/newsearch/category.php?category=shopping](http://tools.search.yahoo.com/newsearch/category.php?category=shopping)

---

<div class="post-metadata">

**Author:** ![Raffa](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@Raffa](https://rubytalk.org/u/Raffa)\
**Post date:** [23 January 2008 17:35 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941/2 "2008-01-23T17:35:04Z")

</div>

cannot use regex, i suppose:

(.{aNumber}).\*\1

where aNumber = 2..x (x=text.length/2), until regex 'response' is  
'nil'

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [23 January 2008 18:08 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941/3 "2008-01-23T18:08:47Z")

</div>

You can use whatever you like.

Try it out on War & Peace though and see how it does. 🙂

James Edward Gray II

> **···**
>
> On Jan 23, 2008, at 11:35 AM, Raffa wrote:
> 
> > cannot use regex, i suppose:
> > 
> > (.{aNumber}).\*\1
> > 
> > where aNumber = 2..x (x=text.length/2), until regex 'response' is  
> > 'nil'

---

<div class="post-metadata">

**Author:** ![SERGEY\_VOLKOV](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@SERGEY\_VOLKOV](https://rubytalk.org/u/SERGEY_VOLKOV)\
**Post date:** [23 January 2008 18:21 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941/4 "2008-01-23T18:21:31Z")

</div>

Why not?  
The simplest solution could be:

def longest\_repeated\_substring str  
&nbsp;&nbsp;&nbsp;&nbsp;(str.size/2).downto(1) { |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/(.{#{i}}).\*\1/m =~ str and return $1  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;nil  
end

but it's too slow;

> **···**
>
> On Jan 23, 2008 12:35 PM, Raffa \<raffaboss@gmail.com\> wrote:
> 
> > cannot use regex, i suppose:
> > 
> > (.{aNumber}).\*\1
> > 
> > where aNumber = 2..x (x=text.length/2), until regex 'response' is  
> > 'nil'

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [24 January 2008 05:39 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941/5 "2008-01-24T05:39:57Z")

</div>

Well, one would think that the absolute simplest solution is /(.\*+).\*\1/,  
but that's what I was testing when I invented the "your banana my banana"  
test case. (Which it failed, returning only "y" as the repeated  
substring).

Yours is a nice extension of this basic idea.

--Ken

> **···**
>
> On Wed, 23 Jan 2008 13:21:31 -0500, SERGEY VOLKOV wrote:
> 
> > Why not?  
> > The simplest solution could be:
> > 
> > def longest\_repeated\_substring str  
> > &nbsp;&nbsp;&nbsp;&nbsp;(str.size/2).downto(1) { |i|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/(.{#{i}}).\*\1/m =~ str and return $1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;nil  
> > end
> > 
> > but it's too slow;
> > 
> > On Jan 23, 2008 12:35 PM, Raffa \<raffaboss@gmail.com\> wrote:
> > 
> > > cannot use regex, i suppose:
> > > 
> > > (.{aNumber}).\*\1
> > > 
> > > where aNumber = 2..x (x=text.length/2), until regex 'response' is 'nil'
> 
> --  
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![SERGEY\_VOLKOV](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@SERGEY\_VOLKOV](https://rubytalk.org/u/SERGEY_VOLKOV)\
**Post date:** [24 January 2008 12:17 UTC](https://rubytalk.org/t/quiz-longest-repeated-substring-153/43941/6 "2008-01-24T12:17:26Z")

</div>

It's linear search for repeated substring length,  
I've tried binary search, too slow too

> **···**
>
> On Jan 24, 2008 12:39 AM, Ken Bloom \<kbloom@gmail.com\> wrote:
> 
> > On Wed, 23 Jan 2008 13:21:31 -0500, SERGEY VOLKOV wrote:
> > 
> > \> Why not?  
> > \> The simplest solution could be:  
> > \>  
> > \> def longest\_repeated\_substring str  
> > \> (str.size/2).downto(1) { |i|  
> > \> /(.{#{i}}).\*\1/m =~ str and return $1  
> > \> }  
> > \> nil  
> > \> end  
> > \>  
> > \> but it's too slow;  
> > \>  
> > \> On Jan 23, 2008 12:35 PM, Raffa \<raffaboss@gmail.com\> wrote:  
> > \>\> cannot use regex, i suppose:  
> > \>\>  
> > \>\> (.{aNumber}).\*\1  
> > \>\>  
> > \>\>  
> > \>\>  
> > \>\> where aNumber = 2..x (x=text.length/2), until regex 'response' is 'nil'
> > 
> > Well, one would think that the absolute simplest solution is /(.\*+).\*\1/,  
> > but that's what I was testing when I invented the "your banana my banana"  
> > test case. (Which it failed, returning only "y" as the repeated  
> > substring).
> > 
> > Yours is a nice extension of this basic idea.
> > 
> > --Ken
> > 
> > --  
> > Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> > Department of Computer Science. Illinois Institute of Technology.  
> > [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)
