titan2400

Longest Substring Without Repeating Characters - LeetCode

Oct 24th, 2025 (edited)
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | Source Code | 0 0
  1. // Longest Substring Without Repeating Characters - https://leetcode.com/problems/longest-substring-without-repeating-characters/description/?source=submission-ac
  2.  
  3. class Solution {
  4.  
  5.     // Time Complexity: O(N^2)
  6.     // Space Complexity; O(k)
  7.     //  where k is the length of longest substring
  8.     // public int lengthOfLongestSubstring(String s) {
  9.  
  10.     //     int maxLength = 0;
  11.     //     for(int i = 0; i < s.length(); i++) {
  12.     //         Set<Character> set = new HashSet<>();
  13.  
  14.     //         set.add(s.charAt(i));
  15.     //         int currentLength = 1;
  16.  
  17.     //         for(int j = i + 1; j < s.length(); j++) {
  18.     //             if(set.contains(s.charAt(j))) {
  19.     //                 break;
  20.     //             }
  21.     //             set.add(s.charAt(j));
  22.     //             currentLength++;
  23.     //         }
  24.  
  25.     //         if(currentLength > maxLength) {
  26.     //             maxLength = currentLength;
  27.     //         }
  28.     //     }
  29.  
  30.     //     return maxLength;
  31.     // }
  32.  
  33.     // Time Complexity: O(n)
  34.     // Space Complexity: O(k)
  35.     //  where n is the string length
  36.     //  and k is the size of the character set
  37.     // public int lengthOfLongestSubstring(String s) {
  38.     //     Set<Character> window = new HashSet<>();
  39.  
  40.     //     int left = 0, right = 0, maxLength = 0;
  41.     //     for(; right < s.length(); right++) {
  42.     //         Character c = s.charAt(right);
  43.  
  44.     //         while(window.contains(c)) {
  45.     //             window.remove(s.charAt(left));
  46.     //             left++;
  47.     //         }
  48.  
  49.     //         window.add(c);
  50.     //         maxLength = Math.max(maxLength, right - left + 1);
  51.     //     }
  52.  
  53.     //     return maxLength;
  54.     // }
  55.  
  56.     // Time Complexity: O(n)
  57.     // Space Complexity: O(k)
  58.     //  where k is the size of the character set
  59.     public int lengthOfLongestSubstring(String s) {
  60.         if (s == null || s.length() == 0) {
  61.             return 0;
  62.         }
  63.  
  64.         // Assuming ASCII character set (128 characters)
  65.         // We store the last index where we saw each character.
  66.         int[] lastSeenIndex = new int[128];
  67.         Arrays.fill(lastSeenIndex, -1); // -1 means "not seen yet"
  68.  
  69.         int maxLength = 0;
  70.         int left = 0; // The left boundary of the sliding window
  71.  
  72.         for (int right = 0; right < s.length(); right++) {
  73.             char c = s.charAt(right);
  74.  
  75.             // Check if the character was seen before and if its last
  76.             // occurrence is inside our current window [left, right].
  77.             if (lastSeenIndex[c] >= left) {
  78.                 // Duplicate found in the window.
  79.                 // Move the left pointer to the position after the last occurrence.
  80.                 left = lastSeenIndex[c] + 1;
  81.             }
  82.  
  83.             // Update the last seen index for this character to the current position.
  84.             lastSeenIndex[c] = right;
  85.  
  86.             // Calculate the length of the current window and update max.
  87.             maxLength = Math.max(maxLength, right - left + 1);
  88.         }
  89.  
  90.         return maxLength;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment