Count occurrences of a string that can be constructed from another given string
Given two strings str1 and str2 where str1 being the parent string. The task is to find out the number of string as str2 that can be constructed using letters of str1.
Note: All the letters are in lowercase and each character should be used only once.
Examples:
Input: str1 = "geeksforgeeks", str2 = "geeks"
Output: 2
Input: str1 = "geekgoinggeeky", str2 = "geeks"
Output: 0
Approach: Store the frequency of characters of str2 in hash2, and do the same for str1 in hash1. Now, find out the minimum value of hash1[i]/hash2[i] for all i where hash2[i]>0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCount(string str1, string str2)
{
int len = str1.size();
int len2 = str2.size();
int ans = INT_MAX;
int hash1[26] = { 0 }, hash2[26] = { 0 };
for ( int i = 0; i < len; i++)
hash1[str1[i] - 'a' ]++;
for ( int i = 0; i < len2; i++)
hash2[str2[i] - 'a' ]++;
for ( int i = 0; i < 26; i++)
if (hash2[i])
ans = min(ans, hash1[i] / hash2[i]);
return ans;
}
int main()
{
string str1 = "geeksclassesatnoida" ;
string str2 = "sea" ;
cout << findCount(str1, str2);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int findCount(String str1, String str2)
{
int len = str1.length();
int len2 = str2.length();
int ans = Integer.MAX_VALUE;
int [] hash1 = new int [ 26 ];
int [] hash2 = new int [ 26 ];
for ( int i = 0 ; i < len; i++)
hash1[( int )(str1.charAt(i) - 'a' )]++;
for ( int i = 0 ; i < len2; i++)
hash2[( int )(str2.charAt(i) - 'a' )]++;
for ( int i = 0 ; i < 26 ; i++)
if (hash2[i] != 0 )
ans = Math.min(ans, hash1[i] / hash2[i]);
return ans;
}
public static void main(String []args)
{
String str1 = "geeksclassesatnoida" ;
String str2 = "sea" ;
System.out.println(findCount(str1, str2));
}
}
|
Python3
import sys
def findCount(str1, str2):
len1 = len (str1)
len2 = len (str2)
ans = sys.maxsize
hash1 = [ 0 ] * 26
hash2 = [ 0 ] * 26
for i in range ( 0 , len1):
hash1[ ord (str1[i]) - 97 ] = hash1[ ord (str1[i]) - 97 ] + 1
for i in range ( 0 , len2):
hash2[ ord (str2[i]) - 97 ] = hash2[ ord (str2[i]) - 97 ] + 1
for i in range ( 0 , 26 ):
if (hash2[i] ! = 0 ):
ans = min (ans, hash1[i] / / hash2[i])
return ans
str1 = "geeksclassesatnoida"
str2 = "sea"
print (findCount(str1, str2))
|
C#
using System;
class GFG
{
static int findCount( string str1, string str2)
{
int len = str1.Length;
int len2 = str2.Length;
int ans = Int32.MaxValue;
int [] hash1 = new int [26];
int [] hash2 = new int [26];
for ( int i = 0; i < len; i++)
hash1[str1[i] - 'a' ]++;
for ( int i = 0; i < len2; i++)
hash2[str2[i] - 'a' ]++;
for ( int i = 0; i < 26; i++)
if (hash2[i] != 0)
ans = Math.Min(ans, hash1[i] / hash2[i]);
return ans;
}
public static void Main()
{
string str1 = "geeksclassesatnoida" ;
string str2 = "sea" ;
Console.WriteLine(findCount(str1, str2));
}
}
|
PHP
<?php
function findCount( $str1 , $str2 )
{
$len = strlen ( $str1 ) ;
$len2 = strlen ( $str1 );
$ans = PHP_INT_MAX;
$hash1 = array_fill (0, 26, 0) ;
$hash2 = array_fill (0, 26, 0);
for ( $i = 0; $i < $len ; $i ++)
$hash1 [ord( $str1 [ $i ]) - ord( 'a' )]++;
for ( $i = 0; $i < $len2 ; $i ++)
$hash2 [ord( $str2 [ $i ]) - ord( 'a' )]++;
for ( $i = 0; $i < 26; $i ++)
if ( $hash2 [ $i ])
$ans = min( $ans , $hash1 [ $i ] / $hash2 [ $i ]);
return $ans ;
}
$str1 = "geeksclassesatnoida" ;
$str2 = "sea" ;
echo findCount( $str1 , $str2 );
?>
|
Javascript
<script>
function findCount(str1, str2) {
var len = str1.length;
var len2 = str2.length;
var ans = 21474836473;
var hash1 = new Array(26).fill(0);
var hash2 = new Array(26).fill(0);
for ( var i = 0; i < len; i++)
hash1[str1[i].charCodeAt(0) - "a" .charCodeAt(0)]++;
for ( var i = 0; i < len2; i++)
hash2[str2[i].charCodeAt(0) - "a" .charCodeAt(0)]++;
for ( var i = 0; i < 26; i++)
if (hash2[i]) ans = Math.min(ans, hash1[i] / hash2[i]);
return ans;
}
var str1 = "geeksclassesatnoida" ;
var str2 = "sea" ;
document.write(findCount(str1, str2));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Last Updated :
15 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...