Count of strings in the first array which are smaller than every string in the second array
Last Updated :
08 Oct, 2023
Given two arrays A[] and B[] which consists of N and M strings respectively. A string S1 is said to be smaller than string S2 if the frequency of the smallest character in the S1 is smaller than the frequency of the smallest character in S2. The task is to count the number of strings in A[] which are smaller than B[i] for every i.
Examples:
Input: A[] = {"aaa", "aa", "bdc"}, B[] = {"cccch", "cccd"}
Output: 3 2
"cccch" has frequency of the smallest character as 4, and all the strings
in A[] have frequencies of the smallest characters less than 4.
"cccd" has frequency of the smallest character as 3 and only "aa" and "bdc"
have frequencies of the smallest character less than 3.
Input: A[] = {"abca", "jji"}, B[] = {"jhgkki", "aaaa", "geeks"}
Output: 0 2 1
Another Method: In this method, We find the number of smaller strings in the array A[] for every string in the array B[]. It uses a nested loop structure to iterate over every string in B[] and A[], and for each string in B[], it calculates the frequency of the smallest character and then iterates over every string in A[] to compare the frequency of the smallest character. If the frequency of the smallest character in A[] is smaller than the frequency of the smallest character in B[], then the count is increased. The final count for every string in B[] is stored in a vector and printed as output.
- Initialize an empty array named ans to store the count of smaller strings in A for every string in B.
- Iterate for every string in B:
- Initialize a variable count to 0 to count the number of smaller strings in A for the current string in B.
- Get the current string in B and store it in a string variable s.
- Calculate the frequency of the smallest character in s by initializing an array freq of size 26 and iterating over the characters in s. Increment the count of the corresponding index in the array for every character. Find the minimum frequency in the array and store it in a variable smallestFreq.
- Iterate for every string in A:
- Initialize a variable t to store the current string in A.
- Calculate the frequency of the smallest character in t by initializing an array freq of size 26 and iterating over the characters in t. Increment the count of the corresponding index in the array for every character. Find the minimum frequency in the array and store it in a variable currFreq.
- Check if the currFreq is less than smallestFreq, if it is, increment the count variable.
- Store the count of smaller strings in A in the ans vector.
- Return the ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of smaller strings in A[]
// for every string in B[]
vector<int> findCount(string a[], string b[], int n, int m)
{
vector<int> ans;
// Iterate for every string in B[]
for (int i = 0; i < m; i++) {
int count = 0;
string s = b[i];
// Calculate the frequency of the smallest character
int freq[26] = { 0 };
for (int j = 0; j < s.length(); j++) {
freq[s[j] - 'a']++;
}
int smallestFreq = INT_MAX;
for (int j = 0; j < 26; j++) {
if (freq[j] > 0) {
smallestFreq = min(smallestFreq, freq[j]);
break;
}
}
// Iterate for every string in A[]
for (int j = 0; j < n; j++) {
string t = a[j];
// Calculate the frequency of the smallest
// character
int freq[26] = { 0 };
for (int k = 0; k < t.length(); k++) {
freq[t[k] - 'a']++;
}
int currFreq = INT_MAX;
for (int k = 0; k < 26; k++) {
if (freq[k] > 0) {
currFreq = min(currFreq, freq[k]);
break;
}
}
// Check if the string in A[] is smaller than
// B[i]
if (currFreq < smallestFreq) {
count++;
}
}
// Store the count of smaller strings in A[]
ans.push_back(count);
}
return ans;
}
// Function to print the answer
void printAnswer(string a[], string b[], int n, int m)
{
// Get the answer
vector<int> ans = findCount(a, b, n, m);
// Print the number of strings for every answer
for (auto it : ans) {
cout << it << " ";
}
}
// Driver code
int main()
{
string A[] = { "aaa", "aa", "bdc" };
string B[] = { "cccch", "cccd" };
int n = sizeof(A) / sizeof(A[0]);
int m = sizeof(B) / sizeof(B[0]);
printAnswer(A, B, n, m);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
// Function to count the number of smaller strings in A[]
// for every string in B[]
public static List<Integer> findCount(String[] a, String[] b, int n, int m) {
List<Integer> ans = new ArrayList<>();
// Iterate for every string in B[]
for (int i = 0; i < m; i++) {
int count = 0;
String s = b[i];
// Calculate the frequency of the smallest character
int[] freq = new int[26];
for (int j = 0; j < s.length(); j++) {
freq[s.charAt(j) - 'a']++;
}
int smallestFreq = Integer.MAX_VALUE;
for (int j = 0; j < 26; j++) {
if (freq[j] > 0) {
smallestFreq = Math.min(smallestFreq, freq[j]);
break;
}
}
// Iterate for every string in A[]
for (int j = 0; j < n; j++) {
String t = a[j];
// Calculate the frequency of the smallest character
int[] charFreq = new int[26];
for (int k = 0; k < t.length(); k++) {
charFreq[t.charAt(k) - 'a']++;
}
int currFreq = Integer.MAX_VALUE;
for (int k = 0; k < 26; k++) {
if (charFreq[k] > 0) {
currFreq = Math.min(currFreq, charFreq[k]);
break;
}
}
// Check if the string in A[] is smaller than B[i]
if (currFreq < smallestFreq) {
count++;
}
}
// Store the count of smaller strings in A[]
ans.add(count);
}
return ans;
}
// Function to print the answer
public static void printAnswer(String[] a, String[] b, int n, int m) {
// Get the answer
List<Integer> ans = findCount(a, b, n, m);
// Print the number of strings for every answer
for (int it : ans) {
System.out.print(it + " ");
}
}
// Driver code
public static void main(String[] args) {
String[] A = { "aaa", "aa", "bdc" };
String[] B = { "cccch", "cccd" };
int n = A.length;
int m = B.length;
printAnswer(A, B, n, m);
}
}
Python3
# code
def find_count(a, b, n, m):
ans = []
# Iterate for every string in B[]
for i in range(m):
count = 0
s = b[i]
# Calculate the frequency of the smallest character
freq = [0] * 26
for j in range(len(s)):
freq[ord(s[j]) - ord('a')] += 1
smallest_freq = float('inf')
for j in range(26):
if freq[j] > 0:
smallest_freq = min(smallest_freq, freq[j])
break
# Iterate for every string in A[]
for j in range(n):
t = a[j]
# Calculate the frequency of the smallest character
char_freq = [0] * 26
for k in range(len(t)):
char_freq[ord(t[k]) - ord('a')] += 1
curr_freq = float('inf')
for k in range(26):
if char_freq[k] > 0:
curr_freq = min(curr_freq, char_freq[k])
break
# Check if the string in A[] is smaller than B[i]
if curr_freq < smallest_freq:
count += 1
# Store the count of smaller strings in A[]
ans.append(count)
return ans
def print_answer(a, b, n, m):
# Get the answer
ans = find_count(a, b, n, m)
# Print the number of strings for every answer
for it in ans:
print(it, end=" ")
# Driver code
def main():
A = ["aaa", "aa", "bdc"]
B = ["cccch", "cccd"]
n = len(A)
m = len(B)
print_answer(A, B, n, m)
if __name__ == "__main__":
main()
#This code is contributed by aeroabrar_31
C#
//C# code for the above approach
using System;
using System.Collections.Generic;
class Program
{
// Function to count the number of smaller strings in A[] for every string in B[]
static List<int> FindCount(string[] a, string[] b, int n, int m)
{
List<int> ans = new List<int>();
// Iterate for every string in B[]
for (int i = 0; i < m; i++)
{
int count = 0;
string s = b[i];
// Calculate the frequency of the smallest character in string s
int[] freq = new int[26];
foreach (char c in s)
{
freq[c - 'a']++;
}
int smallestFreq = int.MaxValue;
foreach (int f in freq)
{
if (f > 0)
{
smallestFreq = Math.Min(smallestFreq, f);
break;
}
}
// Iterate for every string in A[]
for (int j = 0; j < n; j++)
{
string t = a[j];
// Calculate the frequency of the smallest character in string t
int[] currFreq = new int[26];
foreach (char c in t)
{
currFreq[c - 'a']++;
}
int minCurrFreq = int.MaxValue;
foreach (int f in currFreq)
{
if (f > 0)
{
minCurrFreq = Math.Min(minCurrFreq, f);
break;
}
}
// Check if the string in A[] has a smaller frequency than string s
if (minCurrFreq < smallestFreq)
{
count++;
}
}
// Store the count of smaller strings in A[] for the current string in B[]
ans.Add(count);
}
return ans;
}
// Function to print the answer
static void PrintAnswer(string[] a, string[] b, int n, int m)
{
// Get the answer
List<int> ans = FindCount(a, b, n, m);
// Print the number of strings for every answer
foreach (int it in ans)
{
Console.Write(it + " ");
}
}
static void Main()
{
string[] A = { "aaa", "aa", "bdc" };
string[] B = { "cccch", "cccd" };
int n = A.Length;
int m = B.Length;
// Call the function to print the answer
PrintAnswer(A, B, n, m);
}
}
JavaScript
// Function to find the count of smaller strings in A for every string in B
function findCount(a, b) {
const ans = []; // Initialize an empty array to store the results
// Iterate through each string in B
for (let i = 0; i < b.length; i++) {
let count = 0; // Initialize a count variable for each string in B
const s = b[i]; // Get the current string from B
// Initialize an array to keep track of character frequencies (a-z)
const freq = new Array(26).fill(0);
// Count the frequency of each character in the current string s
for (let j = 0; j < s.length; j++) {
freq[s.charCodeAt(j) - 'a'.charCodeAt(0)]++;
}
let smallestFreq = Infinity; // Initialize the smallest character frequency
// Find the smallest character frequency in the current string s
for (let j = 0; j < 26; j++) {
if (freq[j] > 0) {
smallestFreq = Math.min(smallestFreq, freq[j]);
break;
}
}
// Iterate through each string in A
for (let j = 0; j < a.length; j++) {
const t = a[j]; // Get the current string from A
// Initialize an array to keep track of character frequencies (a-z)
const freq = new Array(26).fill(0);
// Count the frequency of each character in the current string t
for (let k = 0; k < t.length; k++) {
freq[t.charCodeAt(k) - 'a'.charCodeAt(0)]++;
}
let currFreq = Infinity; // Initialize the current character frequency
// Find the smallest character frequency in the current string t
for (let k = 0; k < 26; k++) {
if (freq[k] > 0) {
currFreq = Math.min(currFreq, freq[k]);
break;
}
}
// Check if the current string in A has a smaller character frequency
// than the current string in B
if (currFreq < smallestFreq) {
count++;
}
}
// Store the count of smaller strings in A for the current string in B
ans.push(count);
}
return ans; // Return the array of results
}
// Function to print the results
function printAnswer(a, b) {
const ans = findCount(a, b); // Get the results from findCount function
console.log(ans.join(" ")); // Print the results as a space-separated string
}
// Define the input arrays A and B
const A = ["aaa", "aa", "bdc"];
const B = ["cccch", "cccd"];
// Call the printAnswer function to find and print the results
printAnswer(A, B);
Time Complexity: O((N*M)2)
Auxiliary Space: O(1)
An efficient approach is to solve it using Binary Search and some pre-calculations as mentioned below:
- Initially count the frequency of the smallest character of every string and store in the vector/array.
- Sort the vector/array in ascending order.
- Now for every string in B[i], find the frequency of the smallest character.
- Using lower_bound function in C++, or by doing a binary search in the vector/array, find the count of numbers which has frequency smaller than the frequency of the smallest character for every B[i].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define MAX 26
// Function to count the number of smaller
// strings in A[] for every string in B[]
vector<int> findCount(string a[], string b[], int n, int m)
{
// Count the frequency of all characters
int freq[MAX] = { 0 };
vector<int> smallestFreq;
// Iterate for all possible strings in A[]
for (int i = 0; i < n; i++) {
string s = a[i];
memset(freq, 0, sizeof freq);
// Increase the frequency of every character
for (int j = 0; j < s.size(); j++) {
freq[s[j] - 'a']++;
}
// Check for the smallest character's frequency
for (int j = 0; j < MAX; j++) {
// Get the smallest character frequency
if (freq[j]) {
// Insert it in the vector
smallestFreq.push_back(freq[j]);
break;
}
}
}
// Sort the count of all the frequency of the smallest
// character in every string
sort(smallestFreq.begin(), smallestFreq.end());
vector<int> ans;
// Iterate for every string in B[]
for (int i = 0; i < m; i++) {
string s = b[i];
// Hash set every frequency 0
memset(freq, 0, sizeof freq);
// Count the frequency of every character
for (int j = 0; j < s.size(); j++) {
freq[s[j] - 'a']++;
}
int frequency = 0;
// Find the frequency of the smallest character
for (int j = 0; j < MAX; j++) {
if (freq[j]) {
frequency = freq[j];
break;
}
}
// Count the number of strings in A[]
// which has the frequency of the smaller
// character less than the frequency of the
// smaller character of the string in B[]
int ind = lower_bound(smallestFreq.begin(),
smallestFreq.end(), frequency)
- smallestFreq.begin();
// Store the answer
ans.push_back(ind);
}
return ans;
}
// Function to print the answer
void printAnswer(string a[], string b[], int n, int m)
{
// Get the answer
vector<int> ans = findCount(a, b, n, m);
// Print the number of strings
// for every answer
for (auto it : ans) {
cout << it << " ";
}
}
// Driver code
int main()
{
string A[] = { "aaa", "aa", "bdc" };
string B[] = { "cccch", "cccd" };
int n = sizeof(A) / sizeof(A[0]);
int m = sizeof(B) / sizeof(B[0]);
printAnswer(A, B, n, m);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int MAX = 26;
// Function to count the number of smaller
// strings in A[] for every String in B[]
static Vector<Integer> findCount(String a[],
String b[],
int n, int m)
{
// Count the frequency of all characters
int []freq = new int[MAX];
Vector<Integer> smallestFreq = new Vector<Integer>();
// Iterate for all possible strings in A[]
for (int i = 0; i < n; i++)
{
String s = a[i];
Arrays.fill(freq, 0);
// Increase the frequency of every character
for (int j = 0; j < s.length(); j++)
{
freq[s.charAt(j) - 'a']++;
}
// Check for the smallest character's frequency
for (int j = 0; j < MAX; j++)
{
// Get the smallest character frequency
if (freq[j] > 0)
{
// Insert it in the vector
smallestFreq.add(freq[j]);
break;
}
}
}
// Sort the count of all the frequency of
// the smallest character in every string
Collections.sort(smallestFreq);
Vector<Integer> ans = new Vector<Integer>();
// Iterate for every String in B[]
for (int i = 0; i < m; i++)
{
String s = b[i];
// Hash set every frequency 0
Arrays.fill(freq, 0);
// Count the frequency of every character
for (int j = 0; j < s.length(); j++)
{
freq[s.charAt(j) - 'a']++;
}
int frequency = 0;
// Find the frequency of the smallest character
for (int j = 0; j < MAX; j++)
{
if (freq[j] > 0)
{
frequency = freq[j];
break;
}
}
// Count the number of strings in A[]
// which has the frequency of the smaller
// character less than the frequency of the
// smaller character of the String in B[]
int [] array = new int[smallestFreq.size()];
int k = 0;
for(Integer val:smallestFreq)
{
array[k] = val;
k++;
}
int ind = lower_bound(array, 0,
smallestFreq.size(),
frequency);
// Store the answer
ans.add(ind);
}
return ans;
}
static int lower_bound(int[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low) / 2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Function to print the answer
static void printAnswer(String a[], String b[],
int n, int m)
{
// Get the answer
Vector<Integer> ans = findCount(a, b, n, m);
// Print the number of strings
// for every answer
for (Integer it : ans)
{
System.out.print(it + " ");
}
}
// Driver code
public static void main(String[] args)
{
String A[] = { "aaa", "aa", "bdc" };
String B[] = { "cccch", "cccd" };
int n = A.length;
int m = B.length;
printAnswer(A, B, n, m);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
from bisect import bisect_left as lower_bound
MAX = 26
# Function to count the number of smaller
# strings in A for every in B
def findCount(a, b, n, m):
# Count the frequency of all characters
freq=[0 for i in range(MAX)]
smallestFreq=[]
# Iterate for all possible strings in A
for i in range(n):
s = a[i]
for i in range(MAX):
freq[i]=0
# Increase the frequency of every character
for j in range(len(s)):
freq[ord(s[j]) - ord('a')]+= 1
# Check for the smallest character's frequency
for j in range(MAX):
# Get the smallest character frequency
if (freq[j]):
# Insert it in the vector
smallestFreq.append(freq[j])
break
# Sort the count of all the frequency of the smallest
# character in every string
smallestFreq=sorted(smallestFreq)
ans=[]
# Iterate for every in B
for i in range(m):
s = b[i]
# Hash set every frequency 0
for i in range(MAX):
freq[i]=0
# Count the frequency of every character
for j in range(len(s)):
freq[ord(s[j]) - ord('a')]+= 1
frequency = 0
# Find the frequency of the smallest character
for j in range(MAX):
if (freq[j]):
frequency = freq[j]
break
# Count the number of strings in A
# which has the frequency of the smaller
# character less than the frequency of the
# smaller character of the in B
ind = lower_bound(smallestFreq,frequency)
# Store the answer
ans.append(ind)
return ans
# Function to print the answer
def printAnswer(a, b, n, m):
# Get the answer
ans = findCount(a, b, n, m)
# Print the number of strings
# for every answer
for it in ans:
print(it,end=" ")
# Driver code
A = ["aaa", "aa", "bdc"]
B = ["cccch", "cccd"]
n = len(A)
m = len(B)
printAnswer(A, B, n, m)
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
public class GFG
{
static int MAX = 26;
// Function to count the number of smaller
// strings in A[] for every String in B[]
static List<int> findCount(String []a,
String []b,
int n, int m)
{
// Count the frequency of all characters
int []freq = new int[MAX];
List<int> smallestFreq = new List<int>();
// Iterate for all possible strings in A[]
for (int i = 0; i < n; i++)
{
String s = a[i];
for (int l = 0; l < freq.Length; l++)
freq[l]=0;
// Increase the frequency of every character
for (int j = 0; j < s.Length; j++)
{
freq[s[j] - 'a']++;
}
// Check for the smallest character's frequency
for (int j = 0; j < MAX; j++)
{
// Get the smallest character frequency
if (freq[j] > 0)
{
// Insert it in the vector
smallestFreq.Add(freq[j]);
break;
}
}
}
// Sort the count of all the frequency of
// the smallest character in every string
smallestFreq.Sort();
List<int> ans = new List<int>();
// Iterate for every String in B[]
for (int i = 0; i < m; i++)
{
String s = b[i];
// Hash set every frequency 0
for (int l = 0; l < freq.Length; l++)
freq[l]=0;
// Count the frequency of every character
for (int j = 0; j < s.Length; j++)
{
freq[s[j] - 'a']++;
}
int frequency = 0;
// Find the frequency of the smallest character
for (int j = 0; j < MAX; j++)
{
if (freq[j] > 0)
{
frequency = freq[j];
break;
}
}
// Count the number of strings in A[]
// which has the frequency of the smaller
// character less than the frequency of the
// smaller character of the String in B[]
int [] array = new int[smallestFreq.Count];
int k = 0;
foreach (int val in smallestFreq)
{
array[k] = val;
k++;
}
int ind = lower_bound(array, 0,
smallestFreq.Count,
frequency);
// Store the answer
ans.Add(ind);
}
return ans;
}
static int lower_bound(int[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low) / 2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Function to print the answer
static void printAnswer(String []a, String []b,
int n, int m)
{
// Get the answer
List<int> ans = findCount(a, b, n, m);
// Print the number of strings
// for every answer
foreach (int it in ans)
{
Console.Write(it + " ");
}
}
// Driver code
public static void Main(String[] args)
{
String []A = { "aaa", "aa", "bdc" };
String []B = { "cccch", "cccd" };
int n = A.Length;
int m = B.Length;
printAnswer(A, B, n, m);
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// JavaScript implementation of the approach
const MAX = 26;
// Function to count the number of smaller
// strings in A[] for every String in B[]
function findCount(a, b, n, m)
{
// Count the frequency of all characters
var freq = new Array(MAX).fill(0);
var smallestFreq = [];
// Iterate for all possible strings in A[]
for(var i = 0; i < n; i++)
{
var s = a[i];
for(var l = 0; l < freq.length; l++)
freq[l] = 0;
// Increase the frequency of every character
for(var j = 0; j < s.length; j++)
{
freq[s[j].charCodeAt(0) -
"a".charCodeAt(0)]++;
}
// Check for the smallest character's frequency
for(var j = 0; j < MAX; j++)
{
// Get the smallest character frequency
if (freq[j] > 0)
{
// Insert it in the vector
smallestFreq.push(freq[j]);
break;
}
}
}
// Sort the count of all the frequency of
// the smallest character in every string
smallestFreq.sort();
var ans = [];
// Iterate for every String in B[]
for(var i = 0; i < m; i++)
{
var s = b[i];
// Hash set every frequency 0
for(var l = 0; l < freq.length; l++)
freq[l] = 0;
// Count the frequency of every character
for(var j = 0; j < s.length; j++)
{
freq[s[j].charCodeAt(0) -
"a".charCodeAt(0)]++;
}
var frequency = 0;
// Find the frequency of the smallest character
for(var j = 0; j < MAX; j++)
{
if (freq[j] > 0)
{
frequency = freq[j];
break;
}
}
// Count the number of strings in A[]
// which has the frequency of the smaller
// character less than the frequency of the
// smaller character of the String in B[]
var array = new Array(smallestFreq.length).fill(0);
var k = 0;
for(const val of smallestFreq)
{
array[k] = val;
k++;
}
var ind = lower_bound(
array, 0, smallestFreq.length, frequency);
// Store the answer
ans.push(ind);
}
return ans;
}
function lower_bound(a, low, high, element)
{
while (low < high)
{
var middle = low + parseInt((high - low) / 2);
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Function to print the answer
function printAnswer(a, b, n, m)
{
// Get the answer
var ans = findCount(a, b, n, m);
// Print the number of strings
// for every answer
for(const it of ans)
{
document.write(it + " ");
}
}
// Driver code
var A = [ "aaa", "aa", "bdc" ];
var B = [ "cccch", "cccd" ];
var n = A.length;
var m = B.length;
printAnswer(A, B, n, m);
// This code is contributed by rdtank
</script>
Time Complexity: O(n *(log(n) + m)), where n is the size of the array and m is the maximum length of a string in the array.
Auxiliary Space: O(26)
Similar Reads
Count subsequences in first string which are anagrams of the second string
Given two strings str1 and str2 of length n1 and n2 respectively. The problem is to count all the subsequences of str1 which are anagrams of str2. Examples: Input : str1 = "abacd", str2 = "abc" Output : 2 Index of characters in the 2 subsequences are: {0, 1, 3} = {a, b, c} = abc and {1, 2, 3} = {b,
13 min read
Remove characters from the first string which are present in the second string
Given two strings string1 and string2, remove those characters from the first string(string1) which are present in the second string(string2). Both strings are different and contain only lowercase characters.NOTE: The size of the first string is always greater than the size of the second string( |st
15+ min read
Print all strings in the given array that occur as the substring in the given string
Given an array of string arr[] and a string str, the task is to print all the strings in arr[] that occur as a substring in str. Example: Input: str ="geeksforgeeks", arr[] ={ "forg", "geek", "ek", "dog", "sfor"}Output: forggeekeksforExplanation: The strings "forg", "geek", "ek" and "sfor" occur as
5 min read
Check if all K-length subset sums of first array greater than that of the second array
Given two arrays A[] and B[] of size N and an integer K, the task is to check if all possible subset-sums of subsets of size K of the array A[] are greater than that of the array B[] or not. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: A[] = {12, 11, 10, 13}, B[] =
7 min read
Count of times second string can be formed from the characters of first string
Given two strings str and patt, the task is to find the count of times patt can be formed using the characters of str. Examples: Input: str = "geeksforgeeks", patt = "geeks" Output: 2 "geeks" can be made at most twice from the characters of "geeksforgeeks". Input: str = "abcbca", patt = "aabc" Outpu
6 min read
Count of strings that contains every String of another Array as Subset
Given two arrays of strings A[] and B[] containing only small case letters, the task is to count the number of strings in A[] such that it contains every string of array B[] as a subset (ordering of characters does not matter). Examples: Input: A[] = {"geeks", "gfg"}, B[] = {"g", "gf"}Output: 1Expla
12 min read
Count characters of a string which when removed individually makes the string equal to another string
Given two strings A and B of size N and M respectively, the task is to count characters of the string A, which when removed individually makes both the strings equal. If there exists several such characters, then print their respective positions. Otherwise, print "-1". Examples: Input: A = "abaac",
8 min read
Check whether second string can be formed from characters of first string
Given two strings str1 and str2, check if str2 can be formed from str1 Example : Input : str1 = geekforgeeks, str2 = geeksOutput : YesHere, string2 can be formed from string1. Input : str1 = geekforgeeks, str2 = andOutput : NoHere string2 cannot be formed from string1. Input : str1 = geekforgeeks, s
5 min read
Count of strings that does not contain any character of a given string
Given an array arr containing N strings and a string str, the task is to find the number of strings that do not contain any character of string str. Examples: Input: arr[] = {"abcd", "hijk", "xyz", "ayt"}, str="apple"Output: 2Explanation: "hijk" and "xyz" are the strings that do not contain any char
8 min read
Count substrings of same length differing by a single character from two given strings
Given two strings S and T of length N and M respectively, the task is to count the number of ways of obtaining same-length substring from both the strings such that they have a single different character. Examples: Input: S = "ab", T = "bb"Output: 3Explanation: The following are the pairs of substri
7 min read