Find the length of the longest substring without repeating characters in a given string. Return the maximum length as an Integer. For null or empty strings, return 0.

Examples:

Input: s = 'abcabcbb' Output: 3 Explanation: The longest substring without repeating characters is 'abc', with length 3. Input: s = 'bbbbb' Output: 1 Explanation: The longest substring is 'b', with length 1. Input: s = 'pwwkew' Output: 3 Explanation: The longest substring is 'wke', with length 3. Note that 'pwke' is not valid because it is not a substring. Input: s = '' Output: 0 Explanation: Empty string has no characters. Input: s = null Output: 0 Explanation: Null string returns 0. Input: s = 'abcdef' Output: 6 Explanation: The entire string has no repeating characters.
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.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In