Open In App

Python | Strings with similar front and rear character

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Similar front and rear elements
# Using loop
 
# initialize list
test_list = ['gfg', 'is', 'best', 'treat']
 
# printing original list
print("The original list : " + str(test_list))
 
# Similar front and rear elements
# Using loop
count = 0
for ele in test_list:
    if ele[0] == ele[-1]:
        count = count + 1
 
# printing result
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




# Python3 code to demonstrate working of
# Similar front and rear elements
# Using sum() + generator expression
 
# initialize list
test_list = ['gfg', 'is', 'best', 'treat']
 
# printing original list
print("The original list : " + str(test_list))
 
# Similar front and rear elements
# Using sum() + generator expression
res = sum(1 for ele in test_list if ele[0] == ele[-1])
 
# printing result
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




# Python3 code to demonstrate working of
# Similar front and rear elements
# Using loop
 
# initialize list
test_list = ['gfg', 'is', 'best', 'treat']
 
# printing original list
print("The original list : " + str(test_list))
 
# Similar front and rear elements
# Using loop
count = 0
for ele in test_list:
    if ele.startswith(ele[0]) == ele.endswith(ele[0]):
        count = count + 1
 
# printing result
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:

  1. Initialize the list of strings.
  2. Use a list comprehension to create a list of booleans indicating whether each string has similar front and rear elements.
  3. Use the sum() function to count the number of True values in the list.
  4. Print the result.

Python3




# Python3 code to demonstrate working of
# Similar front and rear elements
# Using list comprehension
 
# initialize list
test_list = ['gfg', 'is', 'best', 'treat']
 
# printing original list
print("The original list : " + str(test_list))
 
# Similar front and rear elements
# Using list comprehension
count = sum([ele.startswith(ele[0]) == ele.endswith(ele[0]) for ele in test_list])
 
# printing result
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

  1. Initiate a for loop to traverse list of strings
  2. For each string concatenate first and last character
  3. Check whether occurrence of first character in concatenated string is 2 using count()
  4. If yes increment count by 1 
  5. Display count

Python3




# Python3 code to demonstrate working of
# Similar front and rear elements
# Using loop
 
# initialize list
test_list = ['gfg', 'is', 'best', 'treat']
 
# printing original list
print("The original list : " + str(test_list))
 
# Similar front and rear elements
# Using loop
count = 0
for ele in test_list:
    x=ele[0]+ele[-1]
    if(x.count(ele[0])==2):
        count+=1
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads