SRM 518 DIV 2 TwiceString

0

Problem Statement
    
You are given a string s. Return the shortest string which contains s as a contiguous substring twice.
Note that two occurrences of s may overlap. For example, “ababa” contains “aba” twice.
Definition
    
Class:
TwiceString
Method:
getShortest
Parameters:
string
Returns:
string
Method signature:
string getShortest(string s)
(be sure your method is public)
    

Notes

The shortest string which contains s as a contiguous substring twice is always unique.
Constraints

s will contain between 1 and 50 characters, inclusive.

Each character in s will be a lowercase letter (‘a’-‘z’).
Examples
0)

    
“aba”
Returns: “ababa”
This is the example shown in the problem statement.
1)

    
“xxxxx”
Returns: “xxxxxx”

2)

    
“topcoder”
Returns: “topcodertopcoder”

3)

    
“abracadabra”
Returns: “abracadabracadabra”

 

Solution: 

   If you closly look into the examples you will know tat   you have to find the largest  common prefix and suffix.

i.e in this case “abracadabra”

     largest common prefix and suffix is abra (first 4 characters and last 4 characters)

So run a loop and use inbuilt string functions to find it. After finding it

 concatenate original string with string in which the common prefix is removed(“cadabra” in above example) thats it!!!

 

[gist https://gist.github.com/vishnujayvel/6464654 ]