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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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 Mar/April 2026

Contest runs March 3 - April 30. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.