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
Find Common 2-Character SubstringOriginal: "abcba"abcbaSubstrings: "ab", "bc", "cb", "ba"Reversed: "abcba"abcbaSubstrings: "ab", "bc", "cb", "ba"ababMATCH!Common Substring Found: "ab"Result: truePalindromes naturally have matching substrings in original and reverse
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
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen