Skip to content

C solution to a LeetCode #3 (Longest Substring Without Repeating Characters) problem, fully documented and analyzed with an emphasis on optimal runtime and memory efficiency.

Notifications You must be signed in to change notification settings

ouhidaayoub/longest-substring-without-repeating-characters-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Longest Substring Without Repeating Characters

LeetCode Problem #3

Given a string s, find the length of the longest substring without duplicate characters.

Examples: Input: s = "abcabcbb" Output: 3
Explanation: The answer is "abc", with the length of 3.

Input: s = "bbbbb" Output: 1
Explanation: The answer is "b", with the length of 1.

Input: s = "pwwkew" Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring. "pwke" is a subsequence and not a substring.

Constraints: 0 <= s.length <= 5 × 10⁴
s consists of English letters, digits, symbols, and spaces.

Hint: Generate all possible substrings, check each substring for duplicates, and update the maximum length accordingly.

Test Cases & Results

The following tests were used to verify the correctness of the algorithm:

  • Standard LeetCode examples:

    • Input: 'abcabcbb' → Output: 3
    • Input: 'bbbbb' → Output: 1
    • Input: 'pwwkew' → Output: 3
  • Digits:

    • Input: '1234567890' → Output: 10
  • Symbols:

    • Input: '!@#$^&*()' → Output: 9
  • Mixed letters and digits:

    • Input: 'a1b2c3d4' → Output: 8
  • Mixed case sensitivity:

    • Input: 'AaBbCc' → Output: 6
  • Whitespace:

    • Input: 'a a a a' → Output: 2
  • Complex mixed characters:

    • Input: 'ab@12! cd#' → Output: 10

Notes:

  • All results matched the expected outputs.
  • Tests covered letters, digits, symbols, spaces, and mixed character sets to ensure full ASCII handling.

My quick reaction

Accepted ✅

  • Runtime: 263 ms (~5%) — slow (O(N^2) on purpose this time).
  • Memory: 120 MB (~6%) — heavy (extra buffers/allocs).

I’m not optimizing this one. I wanted correctness and to practice checking all substrings.
Next problems: I’ll push for more efficient solutions (O(N) patterns, less memory).

LeetCode final analysis (screenshots)

Runtime Memory
Time Complexity Space Complexity

About

C solution to a LeetCode #3 (Longest Substring Without Repeating Characters) problem, fully documented and analyzed with an emphasis on optimal runtime and memory efficiency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages