-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestNoRepeatString.java
More file actions
39 lines (36 loc) · 1.15 KB
/
LongestNoRepeatString.java
File metadata and controls
39 lines (36 loc) · 1.15 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
import java.util.*;
public class LongestNoRepeatString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
HashSet<Character> set = new HashSet();
int result = 0;
int count = 0;
int index = 0;
int times = 0;
for (int i = 0; i < s.length(); i++) {
if(times == s.length()-1) break;
if ( !set.add(s.charAt(i) )){
if(s.charAt(i) == s.charAt(i-1)) index++;
set.clear();
if (count > result) result = count;
i = index;
set.add(s.charAt(i));
times ++;
count = 1;
continue;
}
else {
set.add(s.charAt(i) );
index = i;
count++;
times = 0;
if (count > result) result = count;
}
}
System.out.println(result);
}
sc.close();
}
}