计算使用另一个字符串中的单词形成的字符串的出现次数
给定一个字符串A和一个字符串向量B ,任务是计算向量B中仅包含来自A的单词的字符串数。
例子:
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
方法:按照以下步骤解决此问题:
- 提取字符串A的所有单词并将它们存储在一个集合中,比如st 。
- 现在遍历向量B中的每个字符串,并获取该字符串中的所有单词。
- 现在检查是否所有单词都存在于st中,然后将 ans 增加1 。
- 返回ans作为问题的解决方案。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to extract all words from a string
vector getWords(string A)
{
vector words;
string t;
for (int i = 0; i < A.size(); i++) {
// If the character is a space
if (A[i] == ' ') {
if (t.size() > 0) {
words.push_back(t);
}
t = "";
}
// Else
else {
t += A[i];
}
}
// Last word
if (t.size() > 0) {
words.push_back(t);
}
return words;
}
// Function to count the number of strings in B
// that only contains the words from A
int countStrings(string A, vector& B)
{
unordered_set st;
vector words;
words = getWords(A);
for (auto x : words) {
st.insert(x);
}
// Variable to store the final answer
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 all the words are in set st
if (!flag) {
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
string A = "blue green red yellow";
vector B = { "blue red", "green pink", "yellow green" };
cout << countStrings(A, B);
}
Java
// Java code for the above approach
import java.util.*;
class GFG
{
// Function to extract all words from a String
static Vector getWords(String A)
{
Vector words = new Vector();
String t="";
for (int i = 0; i < A.length(); i++) {
// If the character is a space
if (A.charAt(i) == ' ') {
if (t.length() > 0) {
words.add(t);
}
t = "";
}
// Else
else {
t += A.charAt(i);
}
}
// Last word
if (t.length() > 0) {
words.add(t);
}
return words;
}
// Function to count the number of Strings in B
// that only contains the words from A
static int countStrings(String A, String[] B)
{
HashSet st = new HashSet<>();
Vector words = new Vector();
words = getWords(A);
for (String x : words) {
st.add(x);
}
// Variable to store the final answer
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 all the words are in set st
if (!flag) {
ans++;
}
}
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 code for the above approach
# Function to extract all words from a string
def getWords(A):
words = []
t = ""
for i in range(len(A)):
# If the character is a space
if (A[i] == ' '):
if (len(t) > 0):
words.append(t)
t = ""
# Else
else:
t += A[i]
# Last word
if (len(t) > 0):
words.append(t)
return words
# Function to count the number of strings in B
# that only contains the words from A
def countStrings(A, B):
st = set([])
words = []
words = getWords(A)
for x in words:
st.add(x)
# Variable to store the final answer
ans = 0
for x in B:
words = getWords(x)
flag = 0
for y in words:
if (y not in st):
flag = 1
break
# If all the words are in set st
if (not flag):
ans += 1
return ans
# Driver Code
if __name__ == "__main__":
A = "blue green red yellow"
B = ["blue red", "green pink", "yellow green"]
print(countStrings(A, B))
# This code is contributed by ukasp.
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to extract all words from a String
static List getWords(String A)
{
List words = new List();
String t="";
for (int i = 0; i < A.Length; i++) {
// If the character is a space
if (A[i] == ' ') {
if (t.Length > 0) {
words.Add(t);
}
t = "";
}
// Else
else {
t += A[i];
}
}
// Last word
if (t.Length > 0) {
words.Add(t);
}
return words;
}
// Function to count the number of Strings in B
// that only contains the words from A
static int countStrings(String A, String[] B)
{
HashSet st = new HashSet();
List words = new List();
words = getWords(A);
foreach (String x in words) {
st.Add(x);
}
// Variable to store the readonly answer
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 all the words are in set st
if (!flag) {
ans++;
}
}
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
2
时间复杂度: O(N*M),其中 N 是向量 B 的大小,M 是 B 中的最大长度。
辅助空间: O(N)