Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
1,695 views

For proper oa or interview experiences list of all top tech companies visit :

https://oa.desiqna.in

https://interview.desiqna.in

 

image

 

in Online Assessments by Expert (44,360 points)

2 Answers

0 like 0 dislike
 
Best answer

For a string s and an integer k , a selection of substrings is valid if the following conditions are met :

 

  • The length of every substring is greater than or equal to k.
  • Each substring is a palindrome.
  • No two substrings overlap.

 

Determine the maximum number of valid substrings that can be formed from s.

 

Note: A substring is a group of adjacent characters in a string.
A palindrome is a string that reads the same backwards and forwards.

 

Example:
s="aababaabce"
k=3
output-2
strings are aba and baab

 

Constraints:
1<=|s|<=2x10^3
1<=k<=|s|

 

Example 01:
s="ababaeocco"
k=4
output-2
strings are ababa and occo

 

Example 02:
s="aaaaabb"
k=2
output-3
strings are aa , aa and bb

by Expert (44,360 points)
0 like 0 dislike

O(n) solutuon.

 

  1. use Manacher's algorithm to find all the palindrome centers and lengths.
  2. convert centers and lengths to intervals, if a length>k, reduce it to k or k+1. say, if s[4:10] is a palindrome, for k=2, convert it to [6,8]
  3. the intervals should be naturally sorted, and then use sweep line to count the max # of non-overalpped intervals.
by Expert (44,360 points)
...