# LEETCODE 3

#### nico740307

can someone tell me what is wrong with my code, i know there are some useless variables, but i can t figure out what is wrong.
it is the third problem in leetcode and it asks:"Given a string s, find the length of the longest
substring
without repeating characters.". This is my code:

class Solution {
public int lengthOfLongestSubstring(String s) {

int len = s.length();
int len2 = len-1;
int i;
int c = 1;
int k = 0;
int l = 1;
int nulla= 0;
String ag = "";
ArrayList<String> sample = new ArrayList<String>();
ArrayList<Character> sample1 = new ArrayList<Character>();
for(i = 0; i<len2; i++){
char let = s.charAt(i);
char let1 = s.charAt(c);
if(sample1.contains(let)){
nulla++;
}else{

k = i;
l = c;
while(let != let1&& !sample1.contains(let)){
ag = ag + let;
k++;
l++;
let = s.charAt(k);
if(k == len2){
break;
}
let1 = s.charAt(l);
}

ag = "";
if(let != let1){
}
}
c++;
}
String stringa;
int len5;
int len6 = 0;
int maxlen= 0;
int lensa = sample.size();
for(int z = 0; z<lensa; z++){
stringa = sample.get(z);
len5 = stringa.length();
if(len5>=len6){
maxlen = len5;
}else{
maxlen = len6;
}
len6 = len5;
}
return maxlen;

}
}

#### WhiteCube

Do you mean only adjacent repeats, or any repeats?

Z is repeating in FIZZY.

Is Z repeating in TZINKZ?

#### WhiteCube

Here's one way to do it.

The solution can be found in one pass through the string.

Let's say the alphabet is 26 letters. The longest substring with unique letters is somewhere from 1 to 26.

Let p and q be pointers into the string, initialised to 0.

Let X be an array, 'a' to 'z', initialised to -1. X('t') is the last known position of a t.

Let Y be an array, 1 to 26, initialised to false. Y holds the lengths found.

The substring from p to q, is unique.

Let's say you increment q, and q is now pointing to a 't'.

Code:
``````IF X('t') < p THEN
p..q is still unique
Y((q-p)+1) = true
ELSE
p needs to shift
p = X('t') + 1
ENDIF
X('t') = q``````

#### WhiteCube

Sites like leetcode are odd.

Too formal to be a hobby, too unpaid to be a job.

Nothing wrong with programming exercises, but does the world really need 3205 of them?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.