Given a string, find the longest substring that is a palindrome. A palindrome is a sequence of characters that reads the same forwards and backwards.
Your task is to implement the findLongestPalindrome method that takes a string as input and returns the longest palindromic substring.
Requirements
- If there are multiple palindromic substrings of the same maximum length, return the first one found (leftmost)
- Return
nullfornullinput - Return empty string for empty string input
- The comparison should be case-sensitive
- Single characters are considered palindromes
Examples
Example 1:
String input = 'babad'; String result = findLongestPalindrome(input); // Returns: 'bab' (or 'aba' - both are valid, but 'bab' appears first)
Example 2:
String input = 'cbbd'; String result = findLongestPalindrome(input); // Returns: 'bb'
Example 3:
String input = 'racecar'; String result = findLongestPalindrome(input); // Returns: 'racecar' (entire string is a palindrome)
Example 4:
String input = 'a'; String result = findLongestPalindrome(input); // Returns: 'a' (single character is a palindrome)
Example 5:
String input = null; String result = findLongestPalindrome(input); // Returns: null
Example 6:
String input = ''; String result = findLongestPalindrome(input); // Returns: ''
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.