Existence of a Substring in a String and Its Reverse - Problem
Given a string s, find any substring of length 2 which is also present in the reverse of s.
Return true if such a substring exists, and false otherwise.
Note: A substring is a contiguous sequence of characters within a string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abcba"
›
Output:
true
💡 Note:
The 2-character substring "ab" appears at the beginning of the original string and also appears at the beginning of the reversed string "abcba"
Example 2 — No Match
$
Input:
s = "abcd"
›
Output:
false
💡 Note:
Original string has substrings "ab", "bc", "cd". Reversed string "dcba" has substrings "dc", "cb", "ba". No common 2-character substring exists.
Example 3 — Single Match
$
Input:
s = "leetcode"
›
Output:
true
💡 Note:
The substring "ee" appears in the original string. The reversed string "edocteel" also contains "ee", so return true.
Constraints
- 1 ≤ s.length ≤ 100
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string with all its 2-character substrings
2
Create Reverse
Generate reversed string and its 2-character substrings
3
Find Match
Identify any common 2-character substring
Key Takeaway
🎯 Key Insight: Any 2-character substring that appears in both the original string and its reverse indicates the existence of a matching pattern
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code