Skip to content

Commit 84e8127

Browse files
committed
Add time and space complexity documentation to LongestNonRepetitiveSubstring
1 parent 29ce2ef commit 84e8127

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
/**
77
* Class for finding the length of the longest substring without repeating characters.
8+
*
9+
* <p>This implementation uses the sliding window technique combined with a
10+
* {@link java.util.HashMap} to track the last seen index of each character
11+
* while iterating through the string.</p>
812
*/
913
final class LongestNonRepetitiveSubstring {
1014
private LongestNonRepetitiveSubstring() {
@@ -13,6 +17,21 @@ private LongestNonRepetitiveSubstring() {
1317
/**
1418
* Finds the length of the longest substring without repeating characters.
1519
*
20+
* <p>The algorithm maintains a sliding window defined by two pointers.
21+
* As we iterate through the string, we expand the window and update the
22+
* starting position whenever a duplicate character is encountered within
23+
* the current window.</p>
24+
*
25+
* <p><b>Time Complexity:</b> O(n), where n is the length of the input string.
26+
* Each character is processed at most once.</p>
27+
*
28+
* <p><b>Space Complexity:</b> O(min(n, m)), where:
29+
* <ul>
30+
* <li>n = length of the input string</li>
31+
* <li>m = size of the character set (e.g., 128 for ASCII)</li>
32+
* </ul>
33+
* The additional space is used to store character indices in the HashMap.</p>
34+
*
1635
* @param s the input string
1736
* @return the length of the longest non-repetitive substring
1837
*/
@@ -39,4 +58,4 @@ public static int lengthOfLongestSubstring(String s) {
3958

4059
return maxLength;
4160
}
42-
}
61+
}

0 commit comments

Comments
 (0)