给定一个由小写字母和整数K组成的字符串S ,任务是打印字符串S中出现K次的所有单词。
例子:
Input: S = “banana is in yellow and sun flower is also in yellow”, K = 2
Output: “is” “yellow” “in”
Explanation: The words “is”, “yellow” and “in” occurs in the string twice.
Input: S = “geeks for geeks”, K = 2
Output: “geeks”
处理方法:按照以下步骤解决问题:
- 初始化一个列表l来存储字符串出现的单词。
- 拆分单词并将其存储在列表中。
- 遍历列表和每个单词:
- 如果发现单词的频率为K :
- 打印那个词。
- 从列表中删除该单词的当前出现。
- 如果发现单词的频率为K :
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function to print all the words
// occurring k times in a string
void kFreqWords(string S, int K)
{
// Stores the words
string temp = "";
vector l;
for (auto x: S)
{
if(x == ' ')
{
l.push_back(temp);
temp = "";
}
else
temp += x;
}
// Traverse the list
for (auto x: l)
{
// Check for count
if (count(l.begin(), l.end(), x) == K)
{
// Print the word
cout << x << endl;
// Remove from list
remove(l.begin(),l.end(), x);
}
}
}
// Driver Code
int main()
{
// Given string
string S = "banana is in yellow and sun flower is also in yellow ";
// Given value of K
int K = 2;
// Function call to find
// all words occuring K times
kFreqWords(S, K);
}
// This code is contributed by SURENDRA_GANGWAR.
Python3
# Python3 program for the above approach
# Function to print all the words
# occurring k times in a string
def kFreqWords(S, K):
# Stores the words
l = list(S.split(" "))
# Traverse the list
for i in l:
# Check for count
if l.count(i) == K:
# Print the word
print(i)
# Remove from list
l.remove(i)
# Driver Code
if __name__ == "__main__":
# Given string
S = "banana is in yellow and sun flower is also in yellow"
# Given value of K
K = 2
# Function call to find
# all words occuring K times
kFreqWords(S, K)
Python3
# Python program for the above approach
from collections import Counter
# Python program to print words
# which occures k times
def printWords(sentence, k):
# splitting the string
lis = list(sentence.split(" "))
# Calculating frequency of every word
frequency = Counter(lis)
# Traversing the frequency
for i in frequency:
# checking if frequency is k
if(frequency[i] == k):
# print the word
print(i, end=" ")
# Driver code
# Given string
sentence = "sky is blue and my favourite color is blue"
# Given value of K
K = 2
printWords(sentence, K)
# this code is contributed by vikkycirus
输出
is
yellow
in
时间复杂度: O(N)
辅助空间: O(1)
方法#2:使用内置的Python函数:
- 因为句子中的所有单词都用空格分隔。
- 我们必须使用 split() 将句子按空格分割。
- 我们用空格分割所有单词并将它们存储在一个列表中。
- 使用计数器函数来计算单词的频率
- 遍历频率字典并打印频率为k的单词
下面是上述方法的实现:
蟒蛇3
# Python program for the above approach
from collections import Counter
# Python program to print words
# which occures k times
def printWords(sentence, k):
# splitting the string
lis = list(sentence.split(" "))
# Calculating frequency of every word
frequency = Counter(lis)
# Traversing the frequency
for i in frequency:
# checking if frequency is k
if(frequency[i] == k):
# print the word
print(i, end=" ")
# Driver code
# Given string
sentence = "sky is blue and my favourite color is blue"
# Given value of K
K = 2
printWords(sentence, K)
# this code is contributed by vikkycirus
输出:
is blue
时间复杂度: O(N)
空间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live