bigO time complexity of algorithm to find Longest Substring Without Repeating

public int lengthOfLongestSubstring(String s) {

//using a Collection to check if duplicate elements exist.

int count = 0;

int max = 0;

ArrayList str = new ArrayList();

for(int i =0; i< s.length();i++){

String temp = Character.toString(s.charAt(i));

if(str.contains(temp)){

int idx = str.indexOf(temp);

for(int j = 0; j<= idx;j++) // clear all the elements before the duplicate elements, inluding the

str.remove(0); //duplicate. So, no elements collision in the new list.

str.add(temp);

count = str.size(); // update the count - the size of the new list.

}else {

str.add(temp);

count++;

if(count>max)

max=count;

}

}

return max;

}

Hi, what are time and space analysis? I think space complexity is O(n), but time complexity...

Read More »

By: StackOverFlow - 6 days ago

Related Posts