We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 48c17bd commit b7d969dCopy full SHA for b7d969d
LeetCode/String/LongestSubStringWithoutRepetation.java
@@ -0,0 +1,39 @@
1
+/**
2
+ * @author : Piyush Kumar
3
+ * Problem Link : https://leetcode.com/problems/longest-substring-without-repeating-characters/
4
+ */
5
+
6
+class Solution {
7
+ public int lengthOfLongestSubstring(String s) {
8
+ char[] ch = s.toCharArray();
9
+ Map<Character, Integer> map = new HashMap<>();
10
+ int max = ch.length==0 ? 0 : 1;
11
+ int count = 1;
12
+ for (int i=0, j=0; i < ch.length - 1; ) {
13
+ map.put(ch[i], 1);
14
+ if (max == ch.length) {
15
+ break;
16
+ }
17
+ if (ch[j] != ch[j+1] && !map.containsKey(ch[j+1])) {
18
+ map.put(ch[j+1], 1);
19
+ j++;
20
+ count++;
21
+ if (count > max) {
22
+ max = count;
23
24
+ if (j == (ch.length - 1)) {
25
+ i++;
26
+ j=i;
27
+ count=1;
28
29
+ }else {
30
+ map.clear();
31
32
+ j = i;
33
+ count = 1;
34
35
36
+ return max;
37
38
+}
39
0 commit comments