Count occurrences of strings formed using words in another string
Last Updated :
24 Jan, 2022
Given a string A and a vector of strings B, the task is to count the number of strings in vector B that only contains the words from A.
Examples:
Input: A=”blue green red yellow”
B[]={“blue red”, “green pink”, “yellow green”}
Output: 2
Input: A=”apple banana pear”
B[]={“apple”, “banana apple”, “pear banana”}
Output: 3
Approach: Follow the below steps to solve this problem:
- Extract all the words of string A and store them in a set, say st.
- Now traverse on each string in vector B, and get all the words in that string.
- Now check that if all words are present in st, then increment ans by 1.
- Return ans as the solution to the problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<string> getWords(string A)
{
vector<string> words;
string t;
for ( int i = 0; i < A.size(); i++) {
if (A[i] == ' ' ) {
if (t.size() > 0) {
words.push_back(t);
}
t = "" ;
}
else {
t += A[i];
}
}
if (t.size() > 0) {
words.push_back(t);
}
return words;
}
int countStrings(string A, vector<string>& B)
{
unordered_set<string> st;
vector<string> words;
words = getWords(A);
for ( auto x : words) {
st.insert(x);
}
int ans = 0;
for ( auto x : B) {
words = getWords(x);
bool flag = 0;
for ( auto y : words) {
if (st.find(y) == st.end()) {
flag = 1;
break ;
}
}
if (!flag) {
ans++;
}
}
return ans;
}
int main()
{
string A = "blue green red yellow" ;
vector<string> B = { "blue red" , "green pink" , "yellow green" };
cout << countStrings(A, B);
}
|
Java
import java.util.*;
class GFG
{
static Vector<String> getWords(String A)
{
Vector<String> words = new Vector<String>();
String t= "" ;
for ( int i = 0 ; i < A.length(); i++) {
if (A.charAt(i) == ' ' ) {
if (t.length() > 0 ) {
words.add(t);
}
t = "" ;
}
else {
t += A.charAt(i);
}
}
if (t.length() > 0 ) {
words.add(t);
}
return words;
}
static int countStrings(String A, String[] B)
{
HashSet<String> st = new HashSet<>();
Vector<String> words = new Vector<String>();
words = getWords(A);
for (String x : words) {
st.add(x);
}
int ans = 0 ;
for (String x : B) {
words = getWords(x);
boolean flag = false ;
for (String y : words) {
if (!st.contains(y)) {
flag = true ;
break ;
}
}
if (!flag) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
String A = "blue green red yellow" ;
String []B = { "blue red" , "green pink" , "yellow green" };
System.out.print(countStrings(A, B));
}
}
|
Python3
def getWords(A):
words = []
t = ""
for i in range ( len (A)):
if (A[i] = = ' ' ):
if ( len (t) > 0 ):
words.append(t)
t = ""
else :
t + = A[i]
if ( len (t) > 0 ):
words.append(t)
return words
def countStrings(A, B):
st = set ([])
words = []
words = getWords(A)
for x in words:
st.add(x)
ans = 0
for x in B:
words = getWords(x)
flag = 0
for y in words:
if (y not in st):
flag = 1
break
if ( not flag):
ans + = 1
return ans
if __name__ = = "__main__" :
A = "blue green red yellow"
B = [ "blue red" , "green pink" , "yellow green" ]
print (countStrings(A, B))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static List<String> getWords(String A)
{
List<String> words = new List<String>();
String t= "" ;
for ( int i = 0; i < A.Length; i++) {
if (A[i] == ' ' ) {
if (t.Length > 0) {
words.Add(t);
}
t = "" ;
}
else {
t += A[i];
}
}
if (t.Length > 0) {
words.Add(t);
}
return words;
}
static int countStrings(String A, String[] B)
{
HashSet<String> st = new HashSet<String>();
List<String> words = new List<String>();
words = getWords(A);
foreach (String x in words) {
st.Add(x);
}
int ans = 0;
foreach (String x in B) {
words = getWords(x);
bool flag = false ;
foreach (String y in words) {
if (!st.Contains(y)) {
flag = true ;
break ;
}
}
if (!flag) {
ans++;
}
}
return ans;
}
public static void Main(String[] args)
{
String A = "blue green red yellow" ;
String []B = { "blue red" , "green pink" , "yellow green" };
Console.Write(countStrings(A, B));
}
}
|
Javascript
<script>
function getWords(A) {
let words = [];
let t;
for (let i = 0; i < A.length; i++) {
if (A[i] == ' ' ) {
if (t.length > 0) {
words.push(t);
}
t = "" ;
}
else {
t += A[i];
}
}
if (t.length > 0) {
words.push(t);
}
return words;
}
function countStrings(A, B) {
let st = new Set();
let words;
words = getWords(A);
for (let x of words) {
st.add(x);
}
let ans = 0;
for (let x of B) {
words = getWords(x);
let flag = 0;
for (let y = 0; y < words.length; y++) {
if (st.has(words[y])) {
flag = 1;
break ;
}
}
if (flag == 0) {
ans++;
}
}
return ans + 1;
}
let A = "blue green red yellow" ;
let B = [ "blue red" , "green pink" , "yellow green" ];
document.write(countStrings(A, B));
</script>
|
Time Complexity: O(N*M), where N is the size of vector B and M is the maximum length of in B.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...