-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestUniqueSubstring.java
More file actions
95 lines (74 loc) · 1.49 KB
/
LargestUniqueSubstring.java
File metadata and controls
95 lines (74 loc) · 1.49 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class LargestUniqueSubstring {
public static void lues(String s){
boolean arr[]=new boolean[256];
int i=0;
int max=0;
String str2="";
String str;
int t=0;
while(i<s.length()){
i=t;
str="";
while(i<s.length()&&!arr[s.charAt(i)]){
str=str+s.charAt(i);
arr[s.charAt(i)]=true;
i++;
}
if(str.length()>max){
str2=str;
max=str2.length();
}
for(int k=t;k<i;k++){
arr[s.charAt(k)]=false;
}
t++;
}
System.out.println(str2);
}
public static void lues2(String s){
int arr[]=new int[256];
for(int k=0;k<256;k++){
arr[k]=-1;
}
int i=0;
int cs=0;
int max=0;
String str2="";
String str;
boolean check=true;
while(i<s.length()-1){
if(check==false){
str=""+s.charAt(cs);
}else{
check=false;
// System.out.println(i);
str="";
}
while(arr[s.charAt(i)]==-1||arr[s.charAt(i)]<cs){
str=str+s.charAt(i);
// System.out.println(str);
arr[s.charAt(i)]=i;
check=true;
if(i<s.length()-1){
i++;
}
}
if(check==false&&arr[s.charAt(i)]>=cs){
// System.out.println(arr[s.charAt(i)]);
cs=arr[s.charAt(i)]+1;
i=cs+1;
// System.out.println("cs "+s.charAt(i)+" "+cs);
}
if(str.length()>max){
str2=str;
max=str.length();
}
}
System.out.println(str2);
}
public static void main(String[] args) {
String s="bacadbghbjjjjjjc";
lues(s) ;
lues2(s);
}
}