Hash Table String 3083. Existence of a Substring in a String and Its Reverse ¶ Time: $O(n^2)$ Space: $O(n)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10class Solution { public: bool isSubstringPresent(string s) { const string reversed = {s.rbegin(), s.rend()}; for (int i = 0; i + 2 <= s.length(); ++i) if (reversed.find(s.substr(i, 2)) != string::npos) return true; return false; } }; 1 2 3 4 5 6 7 8 9class Solution { public boolean isSubstringPresent(String s) { String reversed = new StringBuilder(s).reverse().toString(); for (int i = 0; i + 2 <= s.length(); ++i) if (reversed.contains(s.substring(i, i + 2))) return true; return false; } } 1 2 3class Solution: def isSubstringPresent(self, s: str) -> bool: return any(s[i:i + 2] in s[::-1] for i in range(len(s) - 1))