aboutsummaryrefslogtreecommitdiff
path: root/LongestSubstringWithoutRepeat.java
blob: d01ecf3273c2ed3439b13a83c3f388d281d4fa57 (plain)
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
import java.lang.*;
import java.util.*;

class LongestSubstringWithoutRepeat {
	public static int lengthOfLongestSubstring(String s) {
		char[] arr = s.toCharArray();
		int max = 0;
		ArrayList<Character> occur = new ArrayList<Character>();
		for (char c : arr) {
			if (!occur.contains(c)) {
				occur.add(c);
			} else {
				occur.clear();
				occur.add(c);
			}
			if (max < occur.size()) {
				max = occur.size();
			}
		}
		return max;
	}

	public static void main(String[] args) {
		String s = "pwwkew";
		System.out.println(lengthOfLongestSubstring(s));
	}
}