Write Python code to count word frequency in a string.
1Times asked
Jul 2026Last seen
Jul 2026First seen
💡 Model Answer
Here is a clean implementation:
python
def count_word_freq(input_string):
frequency = {}
words = input_string.split()
for word in words:
frequency[word] = frequency.get(word, 0) + 1
return frequencyExplanation:
split()breaks the string on whitespace.frequency.get(word, 0)returns the current count or 0 if the word is new.- Complexity is O(n) where n is the number of words; space complexity is O(k) for k unique words.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500