Reverse and Print Common Characters in Two Strings Using Python

This article focuses on a Python program to find common characters between two strings after reversing the second string. It is an efficient solution to tackle scenarios where the position of characters plays a crucial role. This is a common problem in string manipulation and text processing.


Problem Description

Task:
Given two strings, S1 and S2, reverse S2 and find the characters that are common between S1 and the reversed S2 at the same index positions. Both strings will contain only lowercase letters.


Input and Output Format

Input:

  1. The first line contains the string S1.
  2. The second line contains the string S2.

Output:

  1. A single line containing the characters that are common in both strings at the same index positions after reversing S2.

Boundary Conditions

  1. The length of S1 should be between 2 and 500.
  2. The length of S2 should also be between 2 and 500.

Example Input/Output

Example 1:

Input:

energy genuine

Output:

en

Explanation:

  1. Reverse S2:
    Original S2 = genuine, reversed S2 = eniuneg.
  2. Compare S1 and reversed S2 index by index:
    • At index 0, e (from S1) matches e (from reversed S2).
    • At index 1, n (from S1) matches n (from reversed S2).
    • Other indices do not have matching characters.

Hence, the output is en.


Python Code to Reverse and Print Common Characters

# Input strings S1 and S2
s1 = input("Enter string S1: ")
s2 = input("Enter string S2: ")

# Reverse the second string
s2 = s2[::-1]

# Find and print common characters at the same index
common_chars = ""
for i in range(min(len(s1), len(s2))):  # Compare up to the length of the shorter string
    if s1[i] == s2[i]:
        common_chars += s1[i]

print(f"The common characters are: {common_chars}")

Step-by-Step Explanation of the Code

  1. Input the Strings:
    • s1 and s2 are taken as input from the user.
    • For example:
      • Input s1 = "energy".
      • Input s2 = "genuine".
  2. Reverse the Second String:
    • Using slicing (s2[::-1]), we reverse s2.
    • Reversed s2 becomes "eniuneg".
  3. Iterate Through Characters:
    • Use a for loop to iterate over the indices of the shorter string between s1 and s2.
    • Compare characters at the same indices:
      • If s1[i] == s2[i], the characters are appended to the common_chars string.
  4. Output the Result:
    • After completing the loop, the common_chars string contains all the characters that matched at the same indices.

Example Walkthrough

Input:

growth thrown

Steps:

  1. Reverse s2 = thrown.
    Reversed s2 = nworht.
  2. Compare s1 and reversed s2:
    • At index 0: gn → No match.
    • At index 1: rw → No match.
    • At index 2: oo → Match → Add o to common_chars.
    • At index 3: wr → No match.
    • At index 4: th → No match.
    • At index 5: ht → No match.

Output:

o


Edge Cases to Consider

  1. Unequal Length Strings:
    • If S1 and S2 have different lengths, the program compares characters only up to the length of the shorter string.
    • For example: Input: moon planet Output: o
  2. No Matching Characters:
    • If there are no common characters at matching indices, the output will be empty.
    • For example: Input: abc xyz Output: (empty)

Conclusion

This Python program efficiently calculates the common characters between two strings after reversing the second string. By using simple string slicing and looping techniques, it demonstrates a practical approach to string manipulation. This solution can be extended or modified for more complex scenarios in text processing tasks.


Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.