Python – Avoid Spaces in string length
Last Updated :
16 May, 2023
Given a String, compute all the characters, except spaces.
Input : test_str = ‘geeksforgeeks 33 best’
Output : 19
Explanation : Total characters are 19.
Input : test_str = ‘geeksforgeeks best’
Output : 17
Explanation : Total characters are 17 except spaces.
Method #1 : Using isspace() + sum()
In this, we check for each character to be equal not to space() using isspace() and not operator, sum() is used to check frequency.
Python3
test_str = 'geeksforgeeks 33 is best'
print ( "The original string is : " + str (test_str))
res = sum ( not chr .isspace() for chr in test_str)
print ( "The Characters Frequency avoiding spaces : " + str (res))
|
Output
The original string is : geeksforgeeks 33 is best
The Characters Frequency avoiding spaces : 21
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using sum() + len() + map() + split()
In this, we perform split on spaces and extract words without spaces, then the length() of is computed using len() extended to each word using map(), the summation of all lengths computed using sum() is final result.
Python3
test_str = 'geeksforgeeks 33 is best'
print ( "The original string is : " + str (test_str))
res = sum ( map ( len , test_str.split()))
print ( "The Characters Frequency avoiding spaces : " + str (res))
|
Output
The original string is : geeksforgeeks 33 is best
The Characters Frequency avoiding spaces : 21
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using replace() method.
Using replace() we will replace the space in the string with an empty string and then find the length using len() method.
Python3
test_str = 'geeksforgeeks 33 is best'
print ( "The original string is : " + str (test_str))
test_str = test_str.replace( ' ' ,'')
res = len (test_str)
print ( "The Characters Frequency avoiding spaces : " + str (res))
|
Output
The original string is : geeksforgeeks 33 is best
The Characters Frequency avoiding spaces : 21
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using loop and maintaining count variable.
Approach:
- Initialize a count variable with 0.
- Iterate the string character by character using loop.
- If the character is an empty character with space (‘ ‘) then skip that iteration and don’t increase the count variable.
- Other than empty character in string, increase the count variable by 1 in each iteration.
- At the end of loop, print the count variable that shows string length avoiding spaces.
Python3
test_str = 'geeksforgeeks 33 is best'
print ( "The original string is : " , test_str)
count = 0
for i in test_str:
if i = = ' ' :
continue
count + = 1
print ( "The Characters Frequency avoiding spaces : " , count)
|
Output
The original string is : geeksforgeeks 33 is best
The Characters Frequency avoiding spaces : 21
Time Complexity: O(n)
Auxiliary Space: O(n), where n is total characters in string.
Method #5: Using a list comprehension and the join() function
Step-by-step approach:
- Set a counter variable res to 0
- For each character char in test_str, do the following:
- If char is not a whitespace character, increment res by 1
- Return res as the result
Python3
test_str = 'geeksforgeeks 33 is best'
print ( "The original string is : " + str (test_str))
res = len (' '.join([char for char in test_str if char != ' ']))
print ( "The Characters Frequency avoiding spaces : " + str (res))
|
Output
The original string is : geeksforgeeks 33 is best
The Characters Frequency avoiding spaces : 21
Time complexity: O(n), where n is the length of the input string test_str. This is because the algorithm uses a list comprehension to iterate over each character in the string exactly once, and the join() function also iterates over each character in the resulting list exactly once.
Space complexity: O(n), where n is the length of the input string test_str. This is because the algorithm creates a list of non-whitespace characters in the input string, which can be up to the same size as the input string. However, this list is discarded after the join() function is called, so the total space used by the algorithm is still O(n).
Method #6: Using a lambda function and the reduce() method
- Import the functools module.
- Create a lambda function called func that takes in two arguments x and y.
- If the value of y is not a space, the lambda function returns x + 1.
- If the value of y is a space, the lambda function returns x.
- Use the reduce() function from functools module and apply the func lambda function to every element of the test_str string. Start with an initial value of 0.
- Store the result of reduce() in the variable res.
- Print the final result using the print() function. The result is the number of non-space characters in the original string.
Python3
import functools
test_str = 'geeksforgeeks 33 is best'
print ( "The original string is : " + str (test_str))
func = lambda x, y: x + ( 1 if y ! = ' ' else 0 )
res = functools. reduce (func, test_str, 0 )
print ( "The Characters Frequency avoiding spaces : " + str (res))
|
Output
The original string is : geeksforgeeks 33 is best
The Characters Frequency avoiding spaces : 21
Time complexity: O(n)
Auxiliary space: O(1)
Please Login to comment...