educative.io

Alternate solution for Longest Substring with Distinct Characters (hard). Is this right?

public static int findLength(String str) {
// TODO: Write your code here
List chList = new ArrayList<>();
int st = 0, result = 0;
for(int end = 0; end < str.length(); end++) {
char ch = str.charAt(end);

  while(chList.contains(ch))
    chList.remove(Character.valueOf(str.charAt(st++)));

  chList.add(ch);

  result = Math.max(result, end - st + 1);
}
return result;

}


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Longest Substring with Distinct Characters (hard) - Grokking the Coding Interview: Patterns for Coding Questions

Hi @Resika_SB ,

Your code is working fine.