Count of strings that can be formed from another string using each character at-most once
Given two strings str1 and str2, the task is to print the number of times str2 can be formed using characters of str1. However, a character at any index of str1 can only be used once in the formation of str2.
Examples:
Input: str1 = “arajjhupoot”, str2 = “rajput”
Output: 1
str2 can only be formed once using characters of str1.
Input: str1 = “foreeksgekseg”, str2 = “geeks”
Output: 2
Approach: Since the problem has a restriction on using characters of str1 only once to form str2. If one character has been used to form one str2, it cannot be used in forming another str2. Every character of str2 must be present in str1 at least for the formation of one str1. If all the characters of str2 are already present in str1, then the character which has the minimum occurrence in str1 will be the number of str2’s that can be formed using the characters of str1 once. Below are the steps:
- Create an hash-array which stores the number of occurrences of each character of str.
- Iterate for all the characters of str2, and find the minimum most occurrences of every character in str1.
- Return the minimum occurence which will be the answer.
Below is the implementation of the above approach:
C++
/// C++ program to print the number of times // str2 can be formed from str1 using the // characters of str1 only once #include <bits/stdc++.h> using namespace std; // Function to find the number of str2 // that can be formed using characters of str1 int findNumberOfTimes(string str1, string str2) { int freq[26] = { 0 }; int l1 = str1.length(); // iterate and mark the frequencies of // all characters in str1 for (int i = 0; i < l1; i++) freq[str1[i] - 'a'] += 1; int l2 = str2.length(); int count = INT_MAX; // find the minimum frequency of // every character in str1 for (int i = 0; i < l2; i++) count = min(count, freq[str2[i] - 'a']); return count; } // Driver Code int main() { string str1 = "foreeksgekseg"; string str2 = "geeks"; cout << findNumberOfTimes(str1, str2) << endl; return 0; } |
Java
// Java program to print the number of times // str2 can be formed from str1 using the // characters of str1 only once class GFG { // Function to find the number of str2 // that can be formed using characters of str1 static int findNumberOfTimes(String str1, String str2) { int freq[] = new int[26]; int l1 = str1.length(); // iterate and mark the frequencies of // all characters in str1 for (int i = 0; i < l1; i++) { freq[str1.charAt(i) - 'a'] += 1; } int l2 = str2.length(); int count = Integer.MAX_VALUE; // find the minimum frequency of // every character in str1 for (int i = 0; i < l2; i++) { count = Math.min(count, freq[str2.charAt(i) - 'a']); } return count; } public static void main(String[] args) { String str1 = "foreeksgekseg"; String str2 = "geeks"; System.out.println(findNumberOfTimes(str1, str2)); } } /* This code is contributed by 29AjayKumar*/ |
Python3
# Python3 program to print the number of # times str2 can be formed from str1 using # the characters of str1 only once import sys # Function to find the number of str2 # that can be formed using characters of str1 def findNumberOfTimes(str1, str2): freq = [0] * 26 l1 = len(str1) # iterate and mark the frequencies # of all characters in str1 for i in range(l1): freq[ord(str1[i]) - ord("a")] += 1 l2 = len(str2) count = sys.maxsize # find the minimum frequency of # every character in str1 for i in range(l2): count = min(count, freq[ord(str2[i]) - ord('a')]) return count # Driver Code if __name__ == '__main__': str1 = "foreeksgekseg" str2 = "geeks" print(findNumberOfTimes(str1, str2)) # This code is contributed by PrinciRaj1992 |
C#
// C# program to print the number of // times str2 can be formed from str1 // using the characters of str1 only once using System; class GFG { // Function to find the number of // str2 that can be formed using // characters of str1 static int findNumberOfTimes(String str1, String str2) { int []freq = new int[26]; int l1 = str1.Length; // iterate and mark the frequencies // of all characters in str1 for (int i = 0; i < l1; i++) { freq[str1[i] - 'a'] += 1; } int l2 = str2.Length; int count = int.MaxValue; // find the minimum frequency of // every character in str1 for (int i = 0; i < l2; i++) { count = Math.Min(count, freq[str2[i] - 'a']); } return count; } // Driver Code public static void Main() { String str1 = "foreeksgekseg"; String str2 = "geeks"; Console.Write(findNumberOfTimes(str1, str2)); } } // This code is contributed by 29AjayKumar |
PHP
<?php // PHP program to print the number of times // str2 can be formed from str1 using the // characters of str1 only once // Function to find the number of str2 // that can be formed using characters of str1 function findNumberOfTimes($str1, $str2) { $freq = array_fill(0, 26, NULL); $l1 = strlen($str1); // iterate and mark the frequencies // of all characters in str1 for ($i = 0; $i < $l1; $i++) $freq[ord($str1[$i]) - ord('a')] += 1; $l2 = strlen($str2); $count = PHP_INT_MAX; // find the minimum frequency of // every character in str1 for ($i = 0; $i < $l2; $i++) $count = min($count, $freq[ord($str2[$i]) - ord('a')]); return $count; } // Driver Code $str1 = "foreeksgekseg"; $str2 = "geeks"; echo findNumberOfTimes($str1, $str2) . "\n"; // This code is contributed by ita_c ?> |
2
Time Complexity: O(max(l1,l2)), where l1 and l2 are length of str1 and str2 respectively.
Recommended Posts:
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Count of strings that can be formed using a, b and c under given constraints
- Lexicographically smallest string formed by removing at most one character
- All possible strings of any length that can be formed from a given string
- Check if given string can be formed by two other strings or their permutations
- Count of sub-strings that contain character X at least once
- Count of sub-strings that do not consist of the given character
- Find the number of strings formed using distinct characters of a given string
- Pairs of strings which on concatenating contains each character of "string"
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Count occurrences of a character in a repeated string
- Program to count occurrence of a given character in a string
- Count of number of given string in 2D character array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



