Given a string, find the character that has the longest consecutive sequence and return it as a string in the format "character:count". For example, "aabbbcc" has the longest run of "b" with 3 consecutive characters, so the result is "b:3". If there is a tie, return the first one found. Return null for null input and empty string for empty input.
Examples:
Input: str = 'aabbbcc'
Output: 'b:3'
Explanation: b has the longest consecutive run of 3
Input: str = 'aaaa'
Output: 'a:4'
Explanation: Single character repeated 4 times
Input: str = 'aabbcc'
Output: 'a:2'
Explanation: Three characters tie at 2, first one (a) wins
Input: str = 'abcdef'
Output: 'a:1'
Explanation: No repeats, first character wins with count 1
Input: str = null
Output: null
Explanation: Null input returns null
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.