Substring and its reverse in a string

Posted by christa on Stack Overflow See other posts from Stack Overflow or by christa
Published on 2010-03-17T15:08:19Z Indexed on 2010/03/17 15:21 UTC
Read the original article Hit count: 395

Filed under:
|

My professor was talking about this in a Dynamic programming class and asked us to think over it. She gave us some examples as well. Given a string, we were to find the longest continuous subsequence whose reverse is also a subsequence present in the given string.

Example:

INPUT: pqrstuvtsrv
OUTPUT: i=3, k=2

rst -> tsr (rst found first at i=3 and for 2 more positions)

INPUT: mpqrsrqp
OUTPUT: i=2, k=6
pqrsrqp in reverse

INPUT: mmpqssss
OUTPUT: i=5, k=3

I thought of putting the string and its reverse into 2 different arrays and comparing character by character. But I'm sure this is not the best way to do it. Any suggestions as to what could be the most efficient ?

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about string