Python | Strings with similar front and rear character
Last Updated :
12 Apr, 2023
Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop
This is a brute force method by which this task can be performed. In this, iterate each element of a list and check for each string’s front and rear character and increase the counter in case we find a match.
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'treat' ]
print ( "The original list : " + str (test_list))
count = 0
for ele in test_list:
if ele[ 0 ] = = ele[ - 1 ]:
count = count + 1
print ( "Total Strings with similar front and rear elements : " + str (count))
|
Output :
The original list : ['gfg', 'is', 'best', 'treat']
Total Strings with similar front and rear elements : 2
Time Complexity: O(n), where n is the length of the input list. This is because we’re using loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
Method #2: Using sum() + generator expression
This is a one-liner alternative to perform this task. In this, we perform the task of iteration using generator expression and summation using sum().
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'treat' ]
print ( "The original list : " + str (test_list))
res = sum ( 1 for ele in test_list if ele[ 0 ] = = ele[ - 1 ])
print ( "Total Strings with similar front and rear elements : " + str (res))
|
Output :
The original list : ['gfg', 'is', 'best', 'treat']
Total Strings with similar front and rear elements : 2
Method #3: Using startswith() and endswith() methods
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'treat' ]
print ( "The original list : " + str (test_list))
count = 0
for ele in test_list:
if ele.startswith(ele[ 0 ]) = = ele.endswith(ele[ 0 ]):
count = count + 1
print ( "Total Strings with similar front and rear elements : " + str (count))
|
Output
The original list : ['gfg', 'is', 'best', 'treat']
Total Strings with similar front and rear elements : 2
Method 4: Using list comprehension.
Approach:
- Initialize the list of strings.
- Use a list comprehension to create a list of booleans indicating whether each string has similar front and rear elements.
- Use the sum() function to count the number of True values in the list.
- Print the result.
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'treat' ]
print ( "The original list : " + str (test_list))
count = sum ([ele.startswith(ele[ 0 ]) = = ele.endswith(ele[ 0 ]) for ele in test_list])
print ( "Total Strings with similar front and rear elements : " + str (count))
|
Output
The original list : ['gfg', 'is', 'best', 'treat']
Total Strings with similar front and rear elements : 2
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n), because we are creating a list of booleans with n elements.
Method #5 : Using count() method
Approach
- Initiate a for loop to traverse list of strings
- For each string concatenate first and last character
- Check whether occurrence of first character in concatenated string is 2 using count()
- If yes increment count by 1
- Display count
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'treat' ]
print ( "The original list : " + str (test_list))
count = 0
for ele in test_list:
x = ele[ 0 ] + ele[ - 1 ]
if (x.count(ele[ 0 ]) = = 2 ):
count + = 1
print ( "Total Strings with similar front and rear elements : " + str (count))
|
Output
The original list : ['gfg', 'is', 'best', 'treat']
Total Strings with similar front and rear elements : 2
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...