educative.io

My Solution Needs No HashMap

class NoRepeatSubstring {

public static int findLength(String str) {

int start = 0, end = 0, longestSs = Integer.MIN_VALUE;

while(end < str.length()) {

  String currentSs = str.substring(start,end); 

  longestSs = Math.max(longestSs,currentSs.length()); 

  Character incoming = str.charAt(end);

  while(currentSs.indexOf(incoming) != -1) currentSs = str.substring(++start,end);

  end++;

}

return longestSs;

}

}

It seems like you are using Java script substring which is O(n) time from what I recall. That means you worst case runtime would be n * (n +n) since you are doing 2 substrings. O(n * 2n) = O(n^2) which is why hash map is better. O(n^2) runtime wouldn’t pass the interview.

2 Likes

Not to mention the while loop he’s using to repetitively get to the substring that doesn’t contain the incoming character. That makes this solution O(n^3) for the worst case.