Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Avoid Spaces in string length

Python – Avoid Spaces in string length

Given a String, compute all the characters, except spaces.

Input : test_str = ‘neveropen 33 best’ 
Output : 19 
Explanation : Total characters are 19. 

Input : test_str = ‘neveropen 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




# Python3 code to demonstrate working of
# Avoid Spaces in Characters Frequency
# Using isspace() + sum()
 
# initializing string
test_str = 'neveropen 33 is   best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# isspace() checks for space
# sum checks count
res = sum(not chr.isspace() for chr in test_str)
     
# printing result
print("The Characters Frequency avoiding spaces : " + str(res))


Output

The original string is : neveropen 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




# Python3 code to demonstrate working of
# Avoid Spaces in Characters Frequency
# Using sum() + len() + map() + split()
 
# initializing string
test_str = 'neveropen 33 is   best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# len() finds individual word Frequency
# sum() extracts final Frequency
res = sum(map(len, test_str.split()))
     
# printing result
print("The Characters Frequency avoiding spaces : " + str(res))


Output

The original string is : neveropen 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




# Python3 code to demonstrate working of
# Avoid Spaces in Characters Frequency
 
 
# initializing string
test_str = 'neveropen 33 is best'
 
# printing original string
print("The original string is : " + str(test_str))
 
test_str=test_str.replace(' ','')
res=len(test_str)   
# printing result
print("The Characters Frequency avoiding spaces : " + str(res))


Output

The original string is : neveropen 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:

  1. Initialize a count variable with 0.
  2. Iterate the string character by character using loop.
  3. If the character is an empty character with space (‘ ‘) then skip that iteration and don’t increase the count variable.
  4. Other than empty character in string, increase the count variable by 1 in each iteration.
  5. At the end of loop, print the count variable that shows string length avoiding spaces.

Python3




# Python3 code to demonstrate working of
# Avoid Spaces in Characters Frequency
 
 
# initializing string
test_str = 'neveropen 33 is   best'
 
# printing original string
print("The original string is : " , test_str)
 
# initializing count = 0
count = 0
 
# loop to iterate the string, character by character
for i in test_str:
    if i == ' ':
        continue
    count += 1
     
# printing result
print("The Characters Frequency avoiding spaces : " , count)
 
# This code is contributed by Pratik Gupta (guptapratik)


Output

The original string is :  neveropen 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




# initializing string
test_str = 'neveropen 33 is best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using a list comprehension and the join() function
res = len(''.join([char for char in test_str if char != ' ']))
 
# printing result
print("The Characters Frequency avoiding spaces : " + str(res))


Output

The original string is : neveropen 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
 
# Initializing string
test_str = 'neveropen 33 is best'
 
# Printing original string
print("The original string is : " + str(test_str))
 
# Using lambda function and reduce method
func = lambda x, y: x + (1 if y != ' ' else 0)
res = functools.reduce(func, test_str, 0)
 
# Printing result
print("The Characters Frequency avoiding spaces : " + str(res))


Output

The original string is : neveropen 33 is best
The Characters Frequency avoiding spaces : 21

Time complexity: O(n)
Auxiliary space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments