Smallest Palindromic Subsequence of Even Length in Range [L, R]
Last Updated :
31 Dec, 2022
Given a string of size N and some queries, the task is to find the lexicographically smallest palindromic subsequence of even length in range [L, R] for each query. If no such palindromic subsequence exists then the print -1.
Examples:
Input: str = “dbdeke”, query[][] = {{0, 5}, {1, 5}, {1, 3}}
Output: dd
ee
-1
Explanation: dd
In the first query, possible palindromic subsequences are “dd”, “ee”, “ddee” and “dd” which are lexicographically smallest.
In the second query, only possible palindromic subsequence is “ee”.
In the third query, no such palindromic subsequence is possible.
Input: str = “abcd”, query[][] = {{0, 3}}
Output: -1
Approach: The main observation of this problem is if there exists a palindromic subsequence, then it must be of length > 2. Therefore the resultant subsequence will be a string of length > 2 with the same characters. Choose the smallest character among those characters which have a frequency greater than 1 in the range [L, R] and print that character twice. If there is no such character exists then print -1.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int f[26][N];
void precompute(string s, int n)
{
for ( int i = 0; i < n; i++) {
f[s[i] - 'a' ][i]++;
}
for ( int i = 0; i < 26; i++) {
for ( int j = 1; j < n; j++) {
f[i][j] += f[i][j - 1];
}
}
}
int palindromicSubsequencesUtil( int L, int R)
{
int c, ok = 0;
for ( int i = 0; i < 26; i++) {
int cnt = f[i][R];
if (L > 0)
cnt -= f[i][L - 1];
if (cnt > 1) {
ok = 1;
c = i;
break ;
}
}
if (ok == 0) {
return -1;
}
return c;
}
void palindromicSubsequences( int Q[][2], int l)
{
for ( int i = 0; i < l; i++) {
int x
= palindromicSubsequencesUtil(
Q[i][0], Q[i][1]);
if (x == -1) {
cout << -1 << "\n" ;
}
else {
char c = 'a' + x;
cout << c << c << "\n" ;
}
}
}
int main()
{
string str = "dbdeke" ;
int Q[][2] = { { 0, 5 },
{ 1, 5 },
{ 1, 3 } };
int n = str.size();
int l = sizeof (Q) / sizeof (Q[0]);
precompute(str, n);
palindromicSubsequences(Q, l);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = 100001 ;
static int [][]f = new int [ 26 ][N];
static void precompute(String s, int n)
{
for ( int i = 0 ; i < n; i++)
{
f[s.charAt(i) - 'a' ][i]++;
}
for ( int i = 0 ; i < 26 ; i++)
{
for ( int j = 1 ; j < n; j++)
{
f[i][j] += f[i][j - 1 ];
}
}
}
static int palindromicSubsequencesUtil( int L, int R)
{
int c = 0 , ok = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
int cnt = f[i][R];
if (L > 0 )
cnt -= f[i][L - 1 ];
if (cnt > 1 )
{
ok = 1 ;
c = i;
break ;
}
}
if (ok == 0 )
{
return - 1 ;
}
return c;
}
static void palindromicSubsequences( int Q[][], int l)
{
for ( int i = 0 ; i < l; i++)
{
int x = palindromicSubsequencesUtil(
Q[i][ 0 ], Q[i][ 1 ]);
if (x == - 1 )
{
System.out.print(- 1 + "\n" );
}
else
{
char c = ( char ) ( 'a' + x);
System.out.print(( char ) c + "" +
( char ) c + "\n" );
}
}
}
public static void main(String[] args)
{
String str = "dbdeke" ;
int Q[][] = { { 0 , 5 },
{ 1 , 5 },
{ 1 , 3 } };
int n = str.length();
int l = Q.length;
precompute(str, n);
palindromicSubsequences(Q, l);
}
}
|
Python3
N = 100001
f = [[ 0 for x in range (N)]
for y in range ( 26 )]
def precompute(s, n):
for i in range (n):
f[ ord (s[i]) - ord ( 'a' )][i] + = 1
for i in range ( 26 ):
for j in range ( 1 , n):
f[i][j] + = f[i][j - 1 ]
def palindromicSubsequencesUtil(L, R):
ok = 0
for i in range ( 26 ):
cnt = f[i][R]
if (L > 0 ):
cnt - = f[i][L - 1 ]
if (cnt > 1 ):
ok = 1
c = i
break
if (ok = = 0 ):
return - 1
return c
def palindromicSubsequences(Q, l):
for i in range (l):
x = palindromicSubsequencesUtil(Q[i][ 0 ],
Q[i][ 1 ])
if (x = = - 1 ):
print ( - 1 )
else :
c = ord ( 'a' ) + x
print ( 2 * chr (c))
if __name__ = = "__main__" :
st = "dbdeke"
Q = [ [ 0 , 5 ],
[ 1 , 5 ],
[ 1 , 3 ] ]
n = len (st)
l = len (Q)
precompute(st, n)
palindromicSubsequences(Q, l)
|
C#
using System;
class GFG{
static int N = 100001;
static int [,]f = new int [26, N];
static void precompute(String s, int n)
{
for ( int i = 0; i < n; i++)
{
f[s[i] - 'a' , i]++;
}
for ( int i = 0; i < 26; i++)
{
for ( int j = 1; j < n; j++)
{
f[i, j] += f[i, j - 1];
}
}
}
static int palindromicSubsequencesUtil( int L, int R)
{
int c = 0, ok = 0;
for ( int i = 0; i < 26; i++)
{
int cnt = f[i, R];
if (L > 0)
cnt -= f[i, L - 1];
if (cnt > 1)
{
ok = 1;
c = i;
break ;
}
}
if (ok == 0)
{
return -1;
}
return c;
}
static void palindromicSubsequences( int [,]Q, int l)
{
for ( int i = 0; i < l; i++)
{
int x = palindromicSubsequencesUtil(Q[i, 0],
Q[i, 1]);
if (x == -1)
{
Console.Write(-1 + "\n" );
}
else
{
char c = ( char )( 'a' + x);
Console.Write(( char ) c + "" +
( char ) c + "\n" );
}
}
}
public static void Main(String[] args)
{
String str = "dbdeke" ;
int [,]Q = { { 0, 5 },
{ 1, 5 },
{ 1, 3 } };
int n = str.Length;
int l = Q.GetLength(0);
precompute(str, n);
palindromicSubsequences(Q, l);
}
}
|
Javascript
<script>
var N = 100001;
var f = Array.from(Array(26), ()=> Array(N).fill(0));
function precompute(s, n)
{
for ( var i = 0; i < n; i++) {
f[s[i].charCodeAt(0) - 'a' .charCodeAt(0)][i]++;
}
for ( var i = 0; i < 26; i++) {
for ( var j = 1; j < n; j++) {
f[i][j] += f[i][j - 1];
}
}
}
function palindromicSubsequencesUtil(L, R)
{
var c, ok = 0;
for ( var i = 0; i < 26; i++) {
var cnt = f[i][R];
if (L > 0)
cnt -= f[i][L - 1];
if (cnt > 1) {
ok = 1;
c = i;
break ;
}
}
if (ok == 0) {
return -1;
}
return c;
}
function palindromicSubsequences(Q, l)
{
for ( var i = 0; i < l; i++) {
var x
= palindromicSubsequencesUtil(
Q[i][0], Q[i][1]);
if (x == -1) {
document.write( -1 + "<br>" );
}
else {
var c = String.fromCharCode('a'.charCodeAt(0) + x);
document.write( c + c + "<br>" );
}
}
}
var str = "dbdeke" ;
var Q = [ [ 0, 5 ],
[ 1, 5 ],
[ 1, 3 ] ];
var n = str.length;
var l = Q.length;
precompute(str, n);
palindromicSubsequences(Q, l);
</script>
|
Time Complexity: O(26 * N + 26 * Q), where N is the length of string
Auxiliary Space: O(26*N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...