Skip to content

Commit 94a3525

Browse files
authored
Update README with problem explanation and solution
Added a detailed explanation and implementation of the Longest Substring Without Repeating Characters problem in Java.
1 parent 091cbb3 commit 94a3525

File tree

1 file changed

+64
-1
lines changed
  • src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters

1 file changed

+64
-1
lines changed

src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,67 @@ Given a string `s`, find the length of the **longest** **substring** without dup
3131
**Constraints:**
3232

3333
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
34-
* `s` consists of English letters, digits, symbols and spaces.
34+
* `s` consists of English letters, digits, symbols and spaces.
35+
36+
To solve the Longest Substring Without Repeating Characters problem in Java using a `Solution` class, we'll follow these steps:
37+
38+
1. Define a `Solution` class with a method named `lengthOfLongestSubstring`.
39+
2. Initialize variables to keep track of the starting index of the substring (`start`), the maximum length (`maxLen`), and a hashmap to store characters and their indices.
40+
3. Iterate through the string `s`, and for each character:
41+
- Check if the character exists in the hashmap and its index is greater than or equal to the `start` index.
42+
- If found, update the `start` index to the index after the last occurrence of the character.
43+
- Update the maximum length if necessary.
44+
- Update the index of the current character in the hashmap.
45+
4. Return the maximum length found.
46+
5. Handle the edge case where the input string is empty.
47+
48+
Here's the implementation:
49+
50+
```java
51+
import java.util.HashMap;
52+
53+
public class Solution {
54+
55+
public int lengthOfLongestSubstring(String s) {
56+
// Initialize variables
57+
int start = 0;
58+
int maxLen = 0;
59+
HashMap<Character, Integer> map = new HashMap<>();
60+
61+
// Iterate through the string
62+
for (int end = 0; end < s.length(); end++) {
63+
char ch = s.charAt(end);
64+
// If the character exists in the hashmap and its index is greater than or equal to the start index
65+
if (map.containsKey(ch) && map.get(ch) >= start) {
66+
// Update the start index to the index after the last occurrence of the character
67+
start = map.get(ch) + 1;
68+
}
69+
// Update the maximum length if necessary
70+
maxLen = Math.max(maxLen, end - start + 1);
71+
// Update the index of the current character in the hashmap
72+
map.put(ch, end);
73+
}
74+
75+
return maxLen;
76+
}
77+
78+
public static void main(String[] args) {
79+
Solution solution = new Solution();
80+
81+
// Test cases
82+
String s1 = "abcabcbb";
83+
System.out.println("Example 1 Output: " + solution.lengthOfLongestSubstring(s1));
84+
85+
String s2 = "bbbbb";
86+
System.out.println("Example 2 Output: " + solution.lengthOfLongestSubstring(s2));
87+
88+
String s3 = "pwwkew";
89+
System.out.println("Example 3 Output: " + solution.lengthOfLongestSubstring(s3));
90+
91+
String s4 = "";
92+
System.out.println("Example 4 Output: " + solution.lengthOfLongestSubstring(s4));
93+
}
94+
}
95+
```
96+
97+
This implementation provides a solution to the Longest Substring Without Repeating Characters problem in Java.

0 commit comments

Comments
 (0)