Python program to check if a string has at least one letter and one number
Last Updated :
02 May, 2023
Given a string in Python. The task is to check whether the string has at least one letter(character) and one number. Return “True” if the given string fully fill the above condition else return “False” (without quotes).
Examples:
Input: welcome2ourcountry34
Output: True
Input: stringwithoutnum
Output: False
Approach:
The approach is simple we will use loop and two flags for letter and number. These flags will check whether the string contains letter and number. In the end, we will take AND of both flags to check if both are true or not. Letters can be checked in Python String using the isalpha() method and numbers can be checked using the isdigit() method.
Python3
def checkString( str ):
flag_l = False
flag_n = False
for i in str :
if i.isalpha():
flag_l = True
if i.isdigit():
flag_n = True
return flag_l and flag_n
print (checkString( 'thishasboth29' ))
print (checkString( 'geeksforgeeks' ))
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach: Without using builtin methods
Python3
def checkString( str ):
flag_l = False
flag_n = False
for i in str :
if i in "abcdefghijklmnopqrstuvwxyz" :
flag_l = True
if i in "0123456789" :
flag_n = True
return flag_l and flag_n
print (checkString( 'thishasboth29' ))
print (checkString( 'geeksforgeeks' ))
|
Time Complexity: O(n), where n is length of string.
Auxiliary Space: O(1)
Approach : Using operator.countOf() method
Python3
import operator as op
def checkString( str ):
letters = "abcdefghijklmnopqrstuvwxyz"
digits = "0123456789"
flag_l = False
flag_n = False
for i in str :
if op.countOf(letters, i) > 0 :
flag_l = True
if op.countOf(digits, i) > 0 :
flag_n = True
return flag_l and flag_n
print (checkString( 'thishasboth29' ))
print (checkString( 'geeksforgeeks' ))
|
Time Complexity: O(N)
Auxiliary Space : O(1)
Approach : Using regular Expressions
Python3
import re
def checkString( str ):
match = re.search(r '[a-zA-Z]+' , str ) and re.search(r '[0-9]+' , str )
if match:
return True
else :
return False
print (checkString( 'thishasboth29' ))
print (checkString( 'geeksforgeeks' ))
|
Time Complexity: O(N)
Auxiliary Space : O(1)
Approach: Using set intersection:
Algorithm:
1. Define a function “checkString” that takes a string “str” as input.
2. Create two sets – “letters” and “digits”, containing all the letters and digits respectively.
3. Check if the intersection of the input string and the sets of letters and digits is not empty using the ‘&’ operator.
4. Return True if both sets are not empty, otherwise return False.
Python3
def checkString( str ):
letters = set ( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' )
digits = set ( '0123456789' )
return bool (letters & set ( str )) and bool (digits & set ( str ))
print (checkString( 'thishasboth29' ))
print (checkString( 'geeksforgeeks' ))
|
Time Complexity:
The time complexity of this algorithm is O(n), where n is the length of the input string. This is because the sets of letters and digits are constant and have a small size, so their creation can be considered as a constant time operation.
Space Complexity:
The space complexity of the algorithm is O(1), as we are only using two sets to store the letters and digits, which have a constant size. The input string is not modified and no extra space is used to store the output.
Approach: Using lambda function
In this approach, we are using a lambda function to check if the given string contains both letters and numbers. We use the all function to check if all the characters in the string satisfy the given condition, which is that the character should either be an alphabet or a digit. We generate a boolean value for each character in the string using a generator expression inside the all function. Finally, we return the result of all function from the lambda function.
Python3
checkString = lambda s: any (c.isalpha() for c in s) and any (c.isdigit() for c in s)
print (checkString( 'thishasboth29' ))
print (checkString( 'geeksforgeeks' ))
|
Time Complexity: O(n)
Space Complexity: O(1)
Please Login to comment...