-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution3.java
More file actions
53 lines (48 loc) · 1.28 KB
/
solution3.java
File metadata and controls
53 lines (48 loc) · 1.28 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
import java.util.Scanner;
import java.lang.*;
/**
* Created by Lemon on 2017-08-18.
*/
public class solution3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.println(getLongestStr(str));
}
public static int getLongestStr(String str1) {
int[] hashTable = new int[256];
for (int p = 0; p < 256; p++) {
hashTable[p] = 0;
}
char[] str = str1.toCharArray();
int start = 0;
int mstart = 0;
int mlen = 0;
int idx = 0;
int len = 0;
while (idx != str.length) {
if (hashTable[str[idx]] == 1) {
if (len > mlen) {
mstart = start;
System.out.println(str[idx]);
mlen = len;
}
while (str[start] != str[idx]) {
hashTable[str[start]] = 0;
start++;
len--;
}
start++;
} else {
hashTable[str[idx]] = 1;
len++;
}
idx++;
}
if (len > mlen) {
mlen = len;
mstart = start;
}
return mlen;
}
}