Print all words occurring in a sentence exactly K times | Function to print all the words occurring k times in a string ; Stores the words ; Traverse the list ; Check for count ; Print the word ; Remove from list ; Driver Code ; Given string ; Given value of K ; Function call to find all words occuring K times
def kFreqWords ( S , K ) : NEW_LINE INDENT l = list ( S . split ( " ▁ " ) ) NEW_LINE for i in l : NEW_LINE INDENT if l . count ( i ) == K : NEW_LINE INDENT print ( i ) NEW_LINE l . remove ( i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " banana ▁ is ▁ in ▁ yellow ▁ and ▁ sun ▁ flower ▁ is ▁ also ▁ in ▁ yellow " NEW_LINE K = 2 NEW_LINE kFreqWords ( S , K ) NEW_LINE DEDENT