- Joined
- Jun 22, 2024
- Messages
- 1
- Reaction score
- 0
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);
}
sample.add(ag);
ag = "";
if(let != let1){
sample1.add(let);
}
}
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;
}
}
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);
}
sample.add(ag);
ag = "";
if(let != let1){
sample1.add(let);
}
}
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;
}
}