anil is given a sentence as he tries to make a sentence special a sentence can be made special by swapping the character with high frequency with the character having low frequency in the sentence help anil by transforming the sentence into. a special sentence in python
# Python program for the above approach
from collections import Counter
# Python program to print words
# which occurs 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)
Comments
Leave a comment