Given two strings str1 and str2, check if str2 can be formed from str1
Example :
Input : str1 = geekforgeeks, str2 = geeks
Output : Yes
Here, string2 can be formed from string1.
Input : str1 = geekforgeeks, str2 = and
Output : No
Here string2 cannot be formed from string1.
Input : str1 = geekforgeeks, str2 = geeeek
Output : Yes
Here string2 can be formed from string1
as string1 contains ‘e’ comes 4 times in
string2 which is present in string1.
The idea is to count frequencies of characters of str1 in a count array. Then traverse str2 and decrease frequency of characters of str2 in the count array. If frequency of a characters becomes negative at any point, return false.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 256;
bool canMakeStr2(string str1, string str2)
{
int count[MAX] = {0};
for ( int i = 0; i < str1.length(); i++)
count[str1[i]]++;
for ( int i = 0; i < str2.length(); i++)
{
if (count[str2[i]] == 0)
return false ;
count[str2[i]]--;
}
return true ;
}
int main()
{
string str1 = "geekforgeeks" ;
string str2 = "for" ;
if (canMakeStr2(str1, str2))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG {
static int MAX = 256 ;
static boolean canMakeStr2(String str1, String str2)
{
int [] count = new int [MAX];
char []str3 = str1.toCharArray();
for ( int i = 0 ; i < str3.length; i++)
count[str3[i]]++;
char []str4 = str2.toCharArray();
for ( int i = 0 ; i < str4.length; i++) {
if (count[str4[i]] == 0 )
return false ;
count[str4[i]]--;
}
return true ;
}
static public void main(String []args)
{
String str1 = "geekforgeeks" ;
String str2 = "for" ;
if (canMakeStr2(str1, str2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def canMakeStr2(s1, s2):
count = {s1[i] : 0 for i in range ( len (s1))}
for i in range ( len (s1)):
count[s1[i]] + = 1
for i in range ( len (s2)):
if (count.get(s2[i]) = = None or count[s2[i]] = = 0 ):
return False
count[s2[i]] - = 1
return True
s1 = "geekforgeeks"
s2 = "for"
if canMakeStr2(s1, s2):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static int MAX = 256;
static bool canMakeStr2( string str1, string str2)
{
int [] count = new int [MAX];
for ( int i = 0; i < str1.Length; i++)
count[str1[i]]++;
for ( int i = 0; i < str2.Length; i++) {
if (count[str2[i]] == 0)
return false ;
count[str2[i]]--;
}
return true ;
}
static public void Main()
{
string str1 = "geekforgeeks" ;
string str2 = "for" ;
if (canMakeStr2(str1, str2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
$MAX = 256;
function canMakeStr2( $str1 , $str2 )
{
$count = (0);
for ( $i = 0; $i < strlen ( $str1 ); $i ++)
for ( $i = 0; $i < strlen ( $str2 ); $i ++)
{
if ( $count [ $str2 [ $i ]] == 0)
return -1;
}
return true;
}
$str1 = "geekforgeeks" ;
$str2 = "for" ;
if (canMakeStr2( $str1 , $str2 ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function canMakeStr2(str1, str2)
{
let count = {};
for (let i = 0; i < str1.length; i++) {
if (str1[i] in count)
count[str1[i]]++;
else
count[str1[i]] = 1
}
for (let i = 0; i < str2.length; i++) {
if (count[str2[i]] == 0 || !(str2[i] in count))
return false ;
count[str2[i]]--;
}
return true ;
}
let str1 = "geekforgeeks" ;
let str2 = "gggeek" ;
if (canMakeStr2(str1, str2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n+m), where n and m are the length of the given strings.
Auxiliary Space: O(256), which is constant.