Existence of a Substring in a String and Its Reverse - Problem
Given a string s, you need to determine if there exists any substring of length 2 that appears in both the original string and its reverse.
For example, if s = "abcd", its reverse is "dcba". You need to check if any 2-character substring from the original string also exists in the reversed string.
Goal: Return true if such a common substring exists, false otherwise.
Note: A substring is a contiguous sequence of characters within a string. The same substring can appear multiple times and at different positions.
Input & Output
example_1.py โ Basic Case
$
Input:
s = "leetcode"
โบ
Output:
true
๐ก Note:
The substring "ee" appears in both the original string "leetcode" and its reverse "edocteel" (at positions different from each other).
example_2.py โ No Match Case
$
Input:
s = "abcdef"
โบ
Output:
false
๐ก Note:
Original: "abcdef", Reversed: "fedcba". No 2-character substring from original ("ab", "bc", "cd", "de", "ef") appears in reversed ("fe", "ed", "dc", "cb", "ba").
example_3.py โ Palindrome Case
$
Input:
s = "abcba"
โบ
Output:
true
๐ก Note:
The substring "ab" from original appears as "ba" in reverse, and "bc" from original appears as "cb" in reverse. Both are valid matches.
Constraints
- 1 โค s.length โค 1000
- s consists of lowercase English letters only
- Note: If string length < 2, return false as no 2-character substring exists
Visualization
Tap to expand
Understanding the Visualization
1
Read the Secret Message
You have a secret message written on paper
2
Use the Magic Mirror
Hold the message up to a special mirror that shows the reverse
3
Smart Detective Work
Instead of writing down all codes from both sides, you read the original and immediately check if each 2-letter code's reverse was already seen
4
Eureka Moment
The moment you find a match, you've solved the case!
Key Takeaway
๐ฏ Key Insight: The detective doesn't need to write down all reversed codes separately. Instead, for each original code, they immediately check if they've seen its reverse before. This one-pass approach is both elegant and efficient!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code