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:
- The first line contains the string
S1. - The second line contains the string
S2.
Output:
- A single line containing the characters that are common in both strings at the same index positions after reversing
S2.
Boundary Conditions
- The length of
S1should be between 2 and 500. - The length of
S2should also be between 2 and 500.
Example Input/Output
Example 1:
Input:
energy genuine
Output:
en
Explanation:
- Reverse
S2:
OriginalS2 = genuine, reversedS2 = eniuneg. - Compare
S1and reversedS2index by index:- At index 0,
e(fromS1) matchese(from reversedS2). - At index 1,
n(fromS1) matchesn(from reversedS2). - Other indices do not have matching characters.
- At index 0,
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
- Input the Strings:
s1ands2are taken as input from the user.- For example:
- Input
s1 = "energy". - Input
s2 = "genuine".
- Input
- Reverse the Second String:
- Using slicing (
s2[::-1]), we reverses2. - Reversed
s2becomes"eniuneg".
- Using slicing (
- Iterate Through Characters:
- Use a
forloop to iterate over the indices of the shorter string betweens1ands2. - Compare characters at the same indices:
- If
s1[i] == s2[i], the characters are appended to thecommon_charsstring.
- If
- Use a
- Output the Result:
- After completing the loop, the
common_charsstring contains all the characters that matched at the same indices.
- After completing the loop, the
Example Walkthrough
Input:
growth thrown
Steps:
- Reverse
s2 = thrown.
Reverseds2 = nworht. - Compare
s1and reverseds2:- At index 0:
g≠n→ No match. - At index 1:
r≠w→ No match. - At index 2:
o≠o→ Match → Addotocommon_chars. - At index 3:
w≠r→ No match. - At index 4:
t≠h→ No match. - At index 5:
h≠t→ No match.
- At index 0:
Output:
o
Edge Cases to Consider
- Unequal Length Strings:
- If
S1andS2have different lengths, the program compares characters only up to the length of the shorter string. - For example:
Input: moon planet Output: o
- If
- 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.