Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a minimal string meeting some conditions

Recently, I was asked the following problem during an interview.

Given a string S, I need to find another string S2 such that S2 is a subsequence of S and also S is a subsequence of S2+reverse(S2). Here '+' means concatenation. I need to output the min possible length of S2 for given S.

I was told that this is a dynamic programming problem however I was unable to solve it. Can somebody help me with this problem?

EDIT-

Is there a way to do this in O(N2) or less.

like image 205
LTim Avatar asked Apr 27 '16 19:04

LTim


People also ask

How do you search for an element in a string?

You can search for a particular letter in a string using the indexOf() method of the String class. This method which returns a position index of a word within the string if found. Otherwise it returns -1.

How do you check if a string has all characters of another string?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you check if all characters in a string are the same Python?

Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

How do you check if a string contains a character?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.


1 Answers

There are 2 important aspects in this problem.

  1. Since we need S as a substring of S2+reverse(S2), S2 should have atleast n/2 length.
  2. After concatenation of S2 and reverse(S2), there is a pattern where the alphabets repeats such as

enter image description here

So the solution is to check from the center of S to end of S for any consecutive elements. If you find one then check the elements on either side as shown.

enter image description here

Now if you are able to reach till the end of the string, then the minimum number of elements (result) is the distance from start to the point where you find consecutive elements. In this example its C i.e 3.

We know that this may not happen always. i.e you may not be able to find consecutive elements at the center. Let us say the consecutive elements are after the center then we can do the same test.

Main string

enter image description here

Substring

enter image description here

Concatenated string

enter image description here

Now arrives the major doubt. Why we consider only the left side starting from center? The answer is simple, the concatenated string is made by S+reverse(S). So we are sure that the last element in the substring comes consecutive in the concatenated string. There is no way that any repetition in the first half of the main string can give a better result because at least we should have the n alphabets in the final concatenated string

Now the matter of complexity: Searching for consecutive alphabets give a maximum of O(n) Now checking elements on either side iteratively can give a worst case complexity of O(n). i.e maximum n/2 comparisons. We may fail many times doing the second check so the we have a multiplicative relation between the complexities i.e O(n*n).

I believe this is a correct solution and didn't find any loophole yet.

like image 135
Ginu Jacob Avatar answered Oct 01 '22 22:10

Ginu Jacob