Open In App

Python | Ways to check string contain all same characters

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of strings, write a Python program to check whether each string has all the characters same or not. Given below are a few methods to check the same. 

Method #1: Using Naive Method [Inefficient] 

Python3




# Python code to demonstrate
# to check whether string contains
# all characters same or not
 
# Initialising string list
ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"]
 
# Printing initial string
print("Initial Strings list", ini_list)
 
# Using Naive Method:
flag = True
for i in ini_list:
    for j in range(0, len(i)-1):
        if i[j] != i[j + 1]:
            print("String {} don't have all characters same".format(i))
            flag = False
            break
    if flag == True:
        print("String {} have all characters same".format(i))


Output

Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa']
String aaaaaaaaaaa have all characters same
String aaaaaaabaa don't have all characters same

Time Complexity: O(n^2), where n is the length of the longest string in the list.
Auxiliary Space: O(1), as the code only uses a few variables and does not store any additional data.

Method #2: Using String Comparisons 

Python3




# Python code to demonstrate
# to check whether string contains
# all characters same or not
 
# Initialising string list
ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"]
 
# Printing initial string
print("Initial Strings list", ini_list)
 
# Using String comparison
for i in ini_list:
    if i == len(i)*i[0]:
        print("String {} have all characters same".format(i))
    else:
        print("String {} don't have all characters same".format(i))


Output

Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa']
String aaaaaaaaaaa have all characters same
String aaaaaaabaa don't have all characters same

Time Complexity: O(n) where n is the length of the string.
Auxiliary Space: O(1) as we are using only a variable to store the first character of the string and compare it with other characters.

Method #3: Using count comparison 

Python3




# Python code to demonstrate
# to check whether string contains
# all characters same or not
 
# Initialising string list
ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"]
 
# Printing initial string
print("Initial Strings list", ini_list)
 
# Using String comparison
for i in ini_list:
    if i.count(i[0]) == len(i):
        print("String {} have all characters same".format(i))
    else:
        print("String {} don't have all characters same".format(i))


Output

Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa']
String aaaaaaaaaaa have all characters same
String aaaaaaabaa don't have all characters same

Time complexity: O(n), where n is the number of strings in the list ini_list.
Auxiliary space: O(1), as the space used is constant regardless of the size of the input.

Method #4 : Using len() and set() methods

Python3




# Python code to demonstrate
# to check whether string contains
# all characters same or not
 
# Initialising string list
ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"]
 
# Printing initial string
print("Initial Strings list", ini_list)
 
# Using Naive Method:
flag = True
for i in ini_list:
    a = len(set(i))
    if(a == 1):
        print("String {} have all characters same".format(i))
    else:
        print("String {} don't have all characters same".format(i))


Output

Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa']
String aaaaaaaaaaa have all characters same
String aaaaaaabaa don't have all characters same

The time complexity of this code is O(n*m) where n is the length of the list ini_list and m is the length of the longest string in the list. 

The auxiliary space complexity of this code is O(1), as we are not using any extra space to store intermediate results. 

Method #5 : Using all()

Here’s an example of using the built-in all() function to check if a given string contains all the same characters in Python:

In this example, we first initialize a list of strings called ini_list and print it to show the original list of strings. We then use the all() function along with a generator expression to check if all characters in the input string are the same. The generator expression iterates through each character in the input string, comparing it to the first character of the input string.

The all() function returns True if all elements in the input iterable are True, and False otherwise. If the result of the all() function is True, it means that the input string contains all the same characters, and if the result is False, it means that the input string does not contain all the same characters.

Python3




# Initialising string list
ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"]
 
# Printing initial string
print("Initial Strings list", ini_list)
 
# Using all() method
for i in ini_list:
    result = all(c == i[0] for c in i)
    if result:
        print("String {} have all characters same".format(i))
    else:
        print("String {} don't have all characters same".format(i))
#This code is contributed by Edula Vinay Kumar Reddy


Output

Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa']
String aaaaaaaaaaa have all characters same
String aaaaaaabaa don't have all characters same

Time Complexity: O(n) where n is the length of the input string. This is because the method iterates through the input string once to compare each character with the first character of the input string.
Auxiliary Space: O(1) as it doesn’t consume any extra space.



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads