Check if a permutation of S2 can be obtained by adding or removing characters from S1
Given two strings S1 and S2 consisting of N and M characters, the task is to check if the string S1 can be made equal to any permutation of S2 after adding or removing a character a prime number of times from the string S1. If it is possible then print “Yes”. Otherwise, print “No”.
Examples:
Input: S1 = “gekforgk”, S2 = “geeksforgeeks”
Output: Yes
Explanation:
If character ‘e’ is added 3(which is a prime number) times and character ‘s’ is added two(which is a prime number) times to S1 it will be “gekforgkeeess” which is a permutation of S2. Therefore, print Yes.
Input: S1 = “xyzzyzz”, S2 = “xyy”
Output: No
Approach: The given problem can be solved by counting the frequency of characters in strings S1 and S2 and if the difference in any of the frequency of the characters is not prime then print “No”. Otherwise, print “Yes”. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
else if (n == 2)
return true ;
else if (n % 2 == 0)
return false ;
for ( int i = 3;i <= sqrt (n); i += 2)
{
if (n % i == 0)
return false ;
}
return true ;
}
void checkPermutation(string s1, string s2)
{
int freq[26] = {0};
for ( char ch : s1)
{
freq[ch - 'a' ]--;
}
for ( char ch : s2)
{
freq[ch - 'a' ]++;
}
bool isAllChangesPrime = true ;
for ( int i = 0; i < 26; i++)
{
if (freq[i] == 0)
{
continue ;
}
else if (!isPrime( abs (freq[i])))
{
isAllChangesPrime = false ;
break ;
}
}
if (isAllChangesPrime)
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
}
int main()
{
string S1 = "gekforgk" ;
string S2 = "geeksforgeeks" ;
checkPermutation(S1, S2);
}
|
Java
public class GFG {
private static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
else if (n == 2 )
return true ;
else if (n % 2 == 0 )
return false ;
for ( int i = 3 ;
i <= Math.sqrt(n); i += 2 ) {
if (n % i == 0 )
return false ;
}
return true ;
}
private static void checkPermutation(
String s1, String s2)
{
int freq[] = new int [ 26 ];
for ( char ch : s1.toCharArray()) {
freq[ch - 'a' ]--;
}
for ( char ch : s2.toCharArray()) {
freq[ch - 'a' ]++;
}
boolean isAllChangesPrime = true ;
for ( int i = 0 ; i < 26 ; i++) {
if (freq[i] == 0 ) {
continue ;
}
else if (!isPrime(
Math.abs(freq[i]))) {
isAllChangesPrime = false ;
break ;
}
}
if (isAllChangesPrime) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
public static void main(
String[] args)
{
String S1 = "gekforgk" ;
String S2 = "geeksforgeeks" ;
checkPermutation(S1, S2);
}
}
|
Python3
from math import sqrt
def isPrime(n):
if (n < = 1 ):
return False
elif (n = = 2 ):
return True
elif (n % 2 = = 0 ):
return False
for i in range ( 3 , int (sqrt(n)) + 1 , 2 ):
if (n % i = = 0 ):
return False
return True
def checkPermutation(s1, s2):
freq = [ 0 for i in range ( 26 )]
for ch in s1:
if ord (ch) - 97 in freq:
freq[ ord (ch) - 97 ] - = 1
else :
freq[ ord (ch) - 97 ] = 1
for ch in s2:
if ord (ch) - 97 in freq:
freq[ ord (ch) - 97 ] + = 1
else :
freq[ ord (ch) - 97 ] = 1
isAllChangesPrime = True
for i in range ( 26 ):
if (freq[i] = = 0 ):
continue
elif (isPrime( abs (freq[i])) = = False ):
isAllChangesPrime = False
break
if (isAllChangesPrime = = False ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
S1 = "gekforgk"
S2 = "geeksforgeeks"
checkPermutation(S1, S2)
|
C#
using System;
class GFG{
private static bool isPrime( int n)
{
if (n <= 1)
return false ;
else if (n == 2)
return true ;
else if (n % 2 == 0)
return false ;
for ( int i = 3; i <= Math.Sqrt(n); i += 2)
{
if (n % i == 0)
return false ;
}
return true ;
}
private static void checkPermutation(
string s1, string s2)
{
int [] freq = new int [26];
foreach ( char ch in s1.ToCharArray())
{
freq[ch - 'a' ]--;
}
foreach ( char ch in s2.ToCharArray())
{
freq[ch - 'a' ]++;
}
bool isAllChangesPrime = true ;
for ( int i = 0; i < 26; i++)
{
if (freq[i] == 0)
{
continue ;
}
else if (!isPrime(Math.Abs(freq[i])))
{
isAllChangesPrime = false ;
break ;
}
}
if (isAllChangesPrime != false )
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main(String[] args)
{
string S1 = "gekforgk" ;
string S2 = "geeksforgeeks" ;
checkPermutation(S1, S2);
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
else if (n == 2)
return true ;
else if (n % 2 == 0)
return false ;
for (let i = 3;
i <= Math.floor(Math.sqrt(n)); i += 2) {
if (n % i == 0)
return false ;
}
return true ;
}
function checkPermutation(s1, s2)
{
let freq = new Array(26);
for (let i = 0; i < 26; i++)
freq[i] = 0;
for (let ch = 0; ch < s1.length; ch++) {
freq[s1[ch].charCodeAt(0) - 'a' .charCodeAt(0)]--;
}
for (let ch = 0; ch < s2.length; ch++) {
freq[s2[ch].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let isAllChangesPrime = true ;
for (let i = 0; i < 26; i++)
{
if (freq[i] == 0) {
continue ;
}
else if (!isPrime(
Math.abs(freq[i]))) {
isAllChangesPrime = false ;
break ;
}
}
if (isAllChangesPrime) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
let S1 = "gekforgk" ;
let S2 = "geeksforgeeks" ;
checkPermutation(S1, S2);
</script>
|
Time Complexity: O(max(N, M))
Auxiliary Space: O(1)
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...