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!
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 Feb 2026
Contest runs February 1 - 28. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.