Check if characters of a given string can be used to form any N equal strings
Last Updated :
30 Jun, 2021
Given a string S and an integer N, the task is to check if it is possible to generate any string N times from the characters of the given string or not. If it is possible, print Yes. Otherwise, print No.
Examples:
Input: S = “caacbb”, N = 2
Output: Yes
Explanation: All possible strings that can be generated N(= 2) times are {“abc”, “ab”, “aa”, “bb”, “cc”, “bc”, “ca”}
Input: S = “cbacbac”, N = 3
Output: No
Explanation: Since none of the characters occurs N times, therefore no string can be generated N times from the characters of the given string.
Naive Approach: The simplest approach to solve the problem is to generate all possible permutations of all possible lengths of the given string and check if any permutation occurs N times or not. If found to be true, print “Yes”. Otherwise, print “No”
Time Complexity: O(N*L!), where L is the length of the given string.
Auxiliary Space: O(L)
Efficient Approach: The idea is to store the frequency of all characters and count the number of characters occurring at least N times. Follow the steps below to solve the problem:
- Store the frequencies of each character of the string in an array, say freq[].
- Traverse the freq[] array and for every ith Character, check if freq[i] >= N or not.
- If any character is found to be satisfying the above condition, print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSame(string str, int n)
{
map< int , int > mp;
for ( int i = 0; i < str.length(); i++) {
mp[str[i] - 'a' ]++;
}
for ( auto it : mp) {
if ((it.second) >= n) {
return true ;
}
}
return false ;
}
int main()
{
string str = "ccabcba" ;
int n = 4;
if (isSame(str, n)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
|
Java
import java.util.*;
class GFG{
static boolean isSame(String str, int n)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < str.length(); i++)
{
if (mp.containsKey(str.charAt(i) - 'a' ))
{
mp.put(str.charAt(i) - 'a' ,
mp.get(str.charAt(i) - 'a' ) + 1 );
}
else
{
mp.put(str.charAt(i) - 'a' , 1 );
}
}
for (Map.Entry<Integer, Integer> it : mp.entrySet())
{
if ((it.getValue()) >= n)
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
String str = "ccabcba" ;
int n = 4 ;
if (isSame(str, n))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
from collections import defaultdict
def isSame( str , n):
mp = defaultdict( lambda : 0 )
for i in range ( len ( str )):
mp[ ord ( str [i]) - ord ( 'a' )] + = 1
for it in mp.keys():
if (mp[it] > = n):
return True
return False
str = "ccabcba"
n = 4
if (isSame( str , n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isSame(String str, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < str.Length; i++)
{
if (mp.ContainsKey(str[i] - 'a' ))
{
mp[str[i] - 'a' ] =
mp[str[i] - 'a' ] + 1;
}
else
{
mp.Add(str[i] - 'a' , 1);
}
}
foreach (KeyValuePair< int ,
int > it in mp)
{
if ((it.Value) >= n)
{
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
String str = "ccabcba" ;
int n = 4;
if (isSame(str, n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isSame(str, n)
{
var mp = {};
for ( var i = 0; i < str.length; i++)
{
if (mp.hasOwnProperty(str[i].charCodeAt(0) -
"a" .charCodeAt(0)))
{
mp[str[i].charCodeAt(0) - "a" .charCodeAt(0)] =
mp[str[i].charCodeAt(0) - "a" .charCodeAt(0)] + 1;
} else
{
mp[str[i].charCodeAt(0) -
"a" .charCodeAt(0)] = 1;
}
}
for (const [key, value] of Object.entries(mp))
{
if (value >= n)
{
return true ;
}
}
return false ;
}
var str = "ccabcba" ;
var n = 4;
if (isSame(str, n))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(L), where L is the length of the given string
Auxiliary Space: O(L)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...