-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLNDS
More file actions
62 lines (56 loc) · 1.6 KB
/
LNDS
File metadata and controls
62 lines (56 loc) · 1.6 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package 无重复字符的最长字符串;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author junye.mao
*/
public class LNDS {
/**
* @param args the command line arguments
*/
public static int LengthOfLongestSubString(String s){
int length = s.length();
int result = 0, StartInd = 0, EndInd = 0;
Set<Character> substring = new HashSet<>();
while(StartInd < length && EndInd < length){
if(!substring.contains(s.charAt(EndInd))){
substring.add(s.charAt(EndInd++));
result = Math.max(result, EndInd - StartInd);
}else{
substring.remove(s.charAt(StartInd++));
}
}
return result;
}
public int LengthOfLongestSubString2(String s){
int length = s.length();
int result=0 ;
for(int n = 0; n<length; n++){
for(int m = n+1; m<=length; m++){
if(judge(s,n,m))
result = Math.max(result,m-n);
}
}
return result;
}
public boolean judge(String s, int startInd , int endInd){
Set<Character> set = new HashSet<>();
for(int i = startInd; i<endInd; i++){
if(set.contains(s.charAt(startInd))) return false;
set.add(s.charAt(startInd));
}
return true;
}
public static void main(String[] args) {
String a = "aabbddhhsdfe";
int b = LNDS.LengthOfLongestSubString(a);
System.out.println(b);
// TODO code application logic here
}
}