-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge3.java
More file actions
60 lines (51 loc) · 1.83 KB
/
Challenge3.java
File metadata and controls
60 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*Given a string s, find the longest substring without repeating characters */
import java.util.HashMap;
import java.util.Map;
class CharNode {
/*
* This is a class to define the structure of a node to store each character of
* the given string.
*/
private char val; // The character to be stored
private CharNode next; // The refernce to the next node
public CharNode() {
}
public CharNode(char val) {
this.val = val;
}
public CharNode(char val, CharNode next) {
this.val = val;
this.next = next;
}
public static int lengthOfLongestSubString(String s) {
CharNode head = new CharNode();
CharNode temp = head;
for (int i = 0; i < s.length(); i++) { // Creating the linked list
temp.val = s.charAt(i);
temp.next = new CharNode();
temp = temp.next;
}
Map<Character, Integer> string = new HashMap<>(); // Creating a hashmap to store the longest substring
temp = head;
int i = 1; // This is a value to index the characters in the Map
while (temp != null) {
if (temp.next != null) { // This condition is to avoid an OutOfBoundException
if (temp.val != temp.next.val && !string.containsKey(temp.val)) {
string.put(temp.val, i);
} else {
string.remove(temp.val);
}
}
i++;
temp = temp.next;
}
return string.size();
}
}
public class Challenge3 {
public static void main(String[] args) {
// String s = "Hansel";
// System.out.println(s.charAt(1));
System.out.println("Longest substring = " + CharNode.lengthOfLongestSubString("abcabcbb"));
}
}