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.
LeetCode Problem - 3083
class Solution {
// Method to check if a substring of length 2 is present in the reversed string
public boolean isSubstringPresent(String s) {
// Reversing the given string
String reversedString = new StringBuilder(s).reverse().toString();
// Iterating through the original string to check for substrings
for (int i=0; i<s.length()-1; i++){
// Creating a substring of length 2
String temp = String.valueOf(s.charAt(i)) + s.charAt(i + 1);
// Checking if the reversed string contains the substring
if (reversedString.contains(temp)) return true;
}
// If no substring of length 2 is found in the reversed string, return false
return false;
}
}