-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindlongss.java
29 lines (28 loc) · 953 Bytes
/
findlongss.java
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
import java.util.*;
public class findlongss {
public int ss(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashMap<Character, Integer> charMap = new HashMap<>();
int maxL = 0;
int start = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (charMap.containsKey(c)) {
start = Math.max(start, charMap.get(c) + 1);
}
charMap.put(c, i);
maxL = Math.max(maxL, i - start + 1);
}
return maxL;
}
public static void main(String[] args) {
findlongss fls = new findlongss();
Scanner s=new Scanner(System.in);
System.out.println("Enter a string:");
String str = s.nextLine();
System.out.println("The length of the string without any repeating characters is: " + fls.ss(str));
s.close();
}
}