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!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow 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 Jan 2026
Contest runs January 1 - 31. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.