Given an array of words and a string, we need to count all words that are present in given string.
Examples:
Input : words[] = { "welcome", "to", "geeks", "portal"}
str = "geeksforgeeks is a computer science portal for geeks."
Output : 2
Two words "portal" and "geeks" is present in str.
Input : words[] = {"Save", "Water", "Save", "Yourself"}
str = "Save"
Output :1
Steps:
- Extract each word from string.
- For each word, check if it is in word array(by creating set/map). If present, increment result.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countOccurrence(string word[], int n, string str)
{
int counter = 0;
regex reg( "[a-zA-Z]+" );
sregex_iterator it(str.begin(), str.end(), reg);
unordered_set<string> hs;
for ( int i=0; i<n; i++)
hs.insert(word[i]);
while (it != sregex_iterator())
{
if (hs.find(it->str()) != hs.end())
++counter;
it++;
}
return counter;
}
int main()
{
string word[] = { "welcome" , "to" , "geeks" , "portal" };
int n = sizeof (word)/ sizeof (word[0]);
string str = "geeksforgeeks is a computer science portal for geeks." ;
cout << countOccurrence(word, n, str);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
static int countOccurrence(String[] word, String str)
{
int counter = 0 ;
Pattern p = Pattern.compile( "[a-zA-Z]+" );
Matcher m = p.matcher(str);
HashSet<String> hs = new HashSet<String>();
for (String string : word) {
hs.add(string);
}
while (m.find()) {
if (hs.contains(m.group()))
counter++;
}
return counter;
}
public static void main(String[] args)
{
String word[]
= { "welcome" , "to" , "geeks" , "portal" };
String str
= "geeksforgeeks is a computer science portal for geeks." ;
System.out.println(countOccurrence(word, str));
}
}
|
Python3
import re
def countOccurrence(word, n, str ):
counter = 0
reg = re. compile ( "[a-zA-Z]+" )
it = re.finditer(reg, str )
hs = set (word)
for match in it:
if match.group() in hs:
counter + = 1
return counter
word = [ "welcome" , "to" , "geeks" , "portal" ]
n = len (word)
str = "geeksforgeeks is a computer science portal for geeks."
print (countOccurrence(word, n, str ))
|
Javascript
function countOccurrence(word, n, str) {
let counter = 0;
let reg = new RegExp( "[a-zA-Z]+" , "g" );
let match;
let hm = new Map();
for (let i = 0; i < n; i++) {
hm.set(word[i], true );
}
while ((match = reg.exec(str)) !== null ) {
if (hm.has(match[0])) {
counter++;
}
}
return counter;
}
let word = [ "welcome" , "to" , "geeks" , "portal" ];
let n = word.length;
let str = "geeksforgeeks is a computer science portal for geeks." ;
console.log(countOccurrence(word, n, str));
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class GFG{
static void Main( string [] args)
{
string [] word = { "welcome" , "to" , "geeks" , "portal" };
int n = word.Length;
string str = "geeksforgeeks is a computer science portal for geeks." ;
Console.WriteLine(CountOccurrence(word, n, str));
}
static int CountOccurrence( string [] word, int n, string str)
{
int counter = 0;
Regex reg = new Regex( @"[a-zA-Z]+" );
MatchCollection matches = reg.Matches(str);
HashSet< string > hs = new HashSet< string >(word);
foreach (Match match in matches)
{
if (hs.Contains(match.Value))
{
counter++;
}
}
return counter;
}
}
}
|
Time Complexity: O(N), N is the length of the word.
Auxiliary Space: O(1).
Method: First iterate the words using for loop and Check if that word is present in the given string or not if present then increments the count value. Finally, print the count value.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string> w({ "welcome" , "to" , "geeks" , "portal" });
string s = "geeksforgeeks is a computer science portal for geeks." ;
int c = 0;
for ( int i=0; i<w.size(); i++)
{
if (s.find(w[i]) != string::npos)
{
c = c+1;
}
}
cout<< c <<endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
String[] w = { "welcome" , "to" , "geeks" , "portal" };
String s
= "geeksforgeeks is a computer science portal for geeks." ;
int c = 0 ;
for ( int i = 0 ; i < w.length; i++) {
if (s.contains(w[i])) {
c = c + 1 ;
}
}
System.out.println(c);
}
}
|
Python3
w = [ "welcome" , "to" , "geeks" , "portal" ]
s = "geeksforgeeks is a computer science portal for geeks."
c = 0
for i in w:
if i in s:
c + = 1
print (c)
|
C#
using System;
public class GFG {
static public void Main()
{
string [] w = { "welcome" , "to" , "geeks" , "portal" };
string s
= "geeksforgeeks is a computer science portal for geeks." ;
int c = 0;
for ( int i = 0; i < w.Length; i++) {
if (s.Contains(w[i])) {
c = c + 1;
}
}
Console.WriteLine(c);
}
}
|
Javascript
let w= [ "welcome" , "to" , "geeks" , "portal" ];
let s = "geeksforgeeks is a computer science portal for geeks." ;
let c = 0;
for (let i = 0; i < w.length; i++)
{
if (s.indexOf(w[i]) != -1)
{
c = c+1;
}
}
console.log(c)
|
Time Complexity: O(N), N is the size of w.
Auxiliary Space:: O(1).
This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...