Check whether it is possible to permute string such that it does not contain a palindrome of length 2
Last Updated :
20 Feb, 2023
Given a strings S length N consisting of only ‘a’, ‘b’ and ‘c’. The task is to check if it is possible to permute the characters of S such that it will not contain a palindrome of length 2 or more as a substring.
Examples:
Input: S = "abac"
Output: Yes
Explanation :
1. The string contains a palindrome "aba".
2. We can permute the last three characters as follows: S = "acba".
3. Therefore, it does not contain any palindrome of length 2 or more.
Input: S = "aba"
Output: No
Approach: Follow the below steps to solve the problem:
- Traverse through the string.
- For a palindrome of length 2, both the characters should be same- i.e. “aa” or “bb” or “cc”.
- Similarly, for a palindrome of length 3, same letters are separated by another letter. Example – “a ? a” , “b ? b”.
- Therefore, any two same characters must be separated by at least two characters.
- Find the frequency of characters of the string.
- If the difference of count of :
- “a” and “b” is less than 1
- “b” and “c” is less than 1
- “a” and “c” is less than 1
- If all the three conditions are true, return “Yes”
- Else return “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isPossible(string &str)
{
map< char , int > mp;
for ( auto it : str){
mp[it]++;
}
int x = mp[ 'a' ];
int y = mp[ 'b' ];
int z = mp[ 'c' ];
if ( abs (x-y) <= 1 and abs (y-z) <= 1 and abs (x-z) <= 1){
cout << "Yes" << "\n" ;
}
else {
cout << "No" << "\n" ;
}
}
int main()
{
string str = "abac" ;
isPossible(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static void isPossible(String str)
{
HashMap<Character,
Integer> mp = new HashMap<Character,
Integer>();
for ( int i = 0 ; i < str.length(); i++)
{
if (mp.containsKey(str.charAt(i)))
{
mp.put(str.charAt(i),
mp.get(str.charAt(i)) + 1 );
}
else
{
mp.put(str.charAt(i), 1 );
}
}
int x = mp.get( 'a' );
int y = mp.get( 'b' );
int z = mp.get( 'c' );
if (Math.abs(x - y)<= 1 &&
Math.abs(y - z) <= 1 &&
Math.abs(x - z) <= 1 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String[] args)
{
String str = "abac" ;
isPossible(str);
}
}
|
Python3
def isPossible( Str ) :
mp = {}
for it in Str :
if it in mp :
mp[it] + = 1
else :
mp[it] = 1
x = mp[ 'a' ]
y = mp[ 'b' ]
z = mp[ 'c' ]
if ( abs (x - y) < = 1 and abs (y - z) < = 1 and abs (x - z) < = 1 ) :
print ( "Yes" )
else :
print ( "No" )
Str = "abac"
isPossible( Str )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void isPossible( string str)
{
Dictionary< char ,
int > mp = new Dictionary< char ,
int >();
foreach ( char it in str)
{
if (mp.ContainsKey(it))
{
mp[it]++;
}
else
{
mp[it] = 1;
}
}
int x = mp[ 'a' ];
int y = mp[ 'b' ];
int z = mp[ 'c' ];
if (Math.Abs(x - y) <= 1 &&
Math.Abs(y - z) <= 1 &&
Math.Abs(x - z) <= 1)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
static void Main()
{
string str = "abac" ;
isPossible(str);
}
}
|
Javascript
<script>
function isPossible(str)
{
var mp = new Map();
for ( var i = 0; i<str.length; i++)
{
var it = str[i];
if (mp.has(it))
{
mp.set(it, mp.get(it)+1);
}
else
{
mp.set(it, 1);
}
}
var x = mp.get( 'a' );
var y = mp.get( 'b' );
var z = mp.get( 'c' );
if (Math.abs(x-y) <= 1 && Math.abs(y-z) <=
1 && Math.abs(x-z) <= 1){
document.write( "Yes" + "<br>" );
}
else {
document.write( "No" + "<br>" );
}
}
var str = "abac" ;
isPossible(str);
</script>
|
Output:
Yes
Time Complexity : O(N), where N is the length of the string
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...