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.
Example 1:
Input: s = "leetcode"
Output: true
Explanation: Substring "ee"
is of length 2
which is also present in reverse(s) == "edocteel"
.
Example 2:
Input: s = "abcba"
Output: true
Explanation: All of the substrings of length 2
"ab"
, "bc"
, "cb"
, "ba"
are also present in reverse(s) == "abcba"
.
Example 3:
Input: s = "abcd"
Output: false
Explanation: There is no substring of length 2
in s
, which is also present in the reverse of s
.
Constraints:
1 <= s.length <= 100
s
consists only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding if a string contains a substring and its reverse is very straightforward. We essentially check every possible substring within the main string, and then see if the reversed version of that substring also exists somewhere in the main string. We do this exhaustively until we either find a match or have checked every single possibility.
Here's how the algorithm would work step-by-step:
def find_substring_and_reverse(main_string):
string_length = len(main_string)
for substring_length in range(1, string_length + 1):
for i in range(string_length - substring_length + 1):
substring = main_string[i:i + substring_length]
reversed_substring = substring[::-1]
# Check if the reversed substring exists in the main string
if reversed_substring in main_string:
# If it does, we found a match
return True
# If loop completes, no match was found.
return False
The efficient way to solve this problem involves cleverly looking for a string or its reversed version. Instead of checking every possible section of the main string, we use a fast searching algorithm to quickly locate either the original string or its flipped counterpart.
Here's how the algorithm would work step-by-step:
def find_substring_or_reverse(main_string, substring_to_find):
reversed_substring = substring_to_find[::-1]
# Search for the original substring.
if substring_to_find in main_string:
return True
# Search for the reversed substring.
if reversed_substring in main_string:
return True
return False
def existence_of_substring_in_a_string_and_its_reverse(main_string, substring_to_find):
# Handle edge case of empty substring
if not substring_to_find:
return True
return find_substring_or_reverse(main_string, substring_to_find)
def main():
main_string = "abcdefg"
substring_to_find = "bcd"
result = existence_of_substring_in_a_string_and_its_reverse(main_string, substring_to_find)
print(f"Main string: {main_string}, Substring: {substring_to_find}, Result: {result}")
main_string = "abcdefg"
substring_to_find = "gfe"
result = existence_of_substring_in_a_string_and_its_reverse(main_string, substring_to_find)
print(f"Main string: {main_string}, Substring: {substring_to_find}, Result: {result}")
main_string = "abcdefg"
substring_to_find = "xyz"
result = existence_of_substring_in_a_string_and_its_reverse(main_string, substring_to_find)
print(f"Main string: {main_string}, Substring: {substring_to_find}, Result: {result}")
main_string = "abcdefg"
substring_to_find = ""
# Test with empty substring
result = existence_of_substring_in_a_string_and_its_reverse(main_string, substring_to_find)
print(f"Main string: {main_string}, Substring: {substring_to_find}, Result: {result}")
main_string = "abcba"
substring_to_find = "abc"
# Testing a palindrome-like case
result = existence_of_substring_in_a_string_and_its_reverse(main_string, substring_to_find)
print(f"Main string: {main_string}, Substring: {substring_to_find}, Result: {result}")
if __name__ == "__main__":
main()
Case | How to Handle |
---|---|
string1 or string2 is null | Return false immediately since null strings cannot contain substrings. |
string1 or string2 is empty | Return true if string2 is empty and string1 is not null, otherwise return false. |
string2 is longer than string1 | Return false immediately, since a longer string cannot be a substring of a shorter string. |
string1 and string2 are identical | Return true immediately, since the string is a substring of itself. |
string1 contains multiple overlapping occurrences of string2 or its reverse | The algorithm should still correctly return true as it only needs to find one occurrence. |
string2 contains special characters or unicode | The substring search algorithm should work correctly regardless of special characters as it is a character-by-character comparison. |
Very long strings to test for performance | Ensure the chosen algorithm (e.g., using `string.contains()` or similar) is efficient for large string lengths to avoid timeout issues. |
string2 is the reverse of string1 | The reverse check should catch this case and return true. |