Given a string, find the length of the longest substring that contains no repeating characters. A substring is a contiguous sequence of characters within the string. Return 0 for null or empty input.

Examples:

Input: str = 'abcabcbb' Output: 3 Explanation: The longest substring without repeating characters is 'abc' with length 3 Input: str = 'bbbbb' Output: 1 Explanation: The longest substring is 'b' with length 1 Input: str = 'pwwkew' Output: 3 Explanation: The longest substring is 'wke' with length 3 Input: str = '' Output: 0 Explanation: Empty string returns 0 Input: str = 'abcdef' Output: 6 Explanation: All characters are unique, so the entire string is the answer
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 May 2026

Contest runs May 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