Python – Count of Words with specific letter
Given a string of words, extract the count of words with a specific letters.
Input : test_str = ‘geeksforgeeks is best for geeks’, letter = “g”
Output : 2
Explanation : “g” occurs in 2 words.
Input : test_str = ‘geeksforgeeks is best for geeks’, letter = “s”
Output : 4
Explanation : “s” occurs in 4 words.
Method #1 : Using list comprehension + len() + split()
This is one of the ways in which this task can be performed. In this, we perform task of extracting words from string using split() and loop is used to iterate words to check for letter existence of letter and len() is used to fetch the number of words with letter.
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = len ([ele for ele in test_str.split() if letter in ele])
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using filter() + lambda + len() + split()
This is yet another way in which this task can be performed. In this, we perform the task of filtering using filter() + lambda.
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = len ( list ( filter ( lambda ele : letter in ele, test_str.split())))
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3 : Using len() + for loop
We use len() and for loop to iterate over the string to solve the problem.
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
word_count = 0
word = 0
for i in range ( len (test_str)):
if word_count % 2 = = 0 and test_str[i] = = letter:
word + = 1
word_count = 1
elif test_str[i] = = " " :
word_count = 0
print ( "Words count :" , word)
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #4 : Using split() and find()
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = 0
x = test_str.split()
for i in x:
if (i.find(letter)! = - 1 ):
res + = 1
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using split(),replace() and len() methods
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = 0
x = test_str.split()
for i in x:
a = i.replace(letter, "")
if ( len (a) < len (i)):
res + = 1
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
The Time and Space Complexity for all the methods are the same
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using Counter() function
Python3
from collections import Counter
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = 0
x = test_str.split()
for i in x:
freq = Counter(i)
if letter in freq.keys():
res + = 1
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using operator.countOf() method
Python3
import operator as op
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = 0
x = test_str.split()
for i in x:
if op.countOf(i, letter) > 0 :
res + = 1
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #8 : Using split() and set():
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
words = set (test_str.split())
count = 0
for word in words:
if letter in word:
count + = 1
print ( "Words count : " + str (count))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity: O(N)
Space Complexity : O(1)
Method#9: Using Recursion method.
Python3
def count_words_with_letter(string, letter, count = 0 ):
if not string:
return count
if letter in string.split()[ 0 ]:
count + = 1
return count_words_with_letter(string[ len (string.split()[ 0 ]) + 1 :], letter, count)
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
result = count_words_with_letter(test_str, letter)
print ( "Words count:" , result)
|
Output
The original string is : geeksforgeeks is best for geeks
Words count: 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #10 : Using re module
This approach uses the re (regular expression) module in python to find all occurrences of the given letter in the string and counting the number of matches.
Python3
import re
def count_words_with_letter(test_str, letter):
words = test_str.split()
count = sum ([ 1 for word in words if re.match(f '.*{letter}.*' , word)])
return count
test_str = 'geeksforgeeks is best for geeks'
letter = 'e'
print (count_words_with_letter(test_str, letter))
|
Time Complexity: O(n * m), where n is the number of words in the input string and m is the length of each word on average. This is because the re.match function performs a search operation on each word in the input string, and the length of each search operation is proportional to the length of the word.
Auxiliary Space: O(1)
Method #11 : Using operator.contains() method
Approach
- Split the string and it turns to list
- Initiated a for loop to traverse the list
- Checked whether the letter is present in each element of list using operator.contains()
- If yes increment the result variable
- Display the result variable
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
letter = "e"
res = 0
x = test_str.split()
import operator
for i in x:
if (operator.contains(i,letter)):
res + = 1
print ( "Words count : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
Words count : 3
Time Complexity : O(N)
Auxiliary Space : O(1)
Last Updated :
25 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...