Currently I'm learning about algorithm efficiency in terms of time and space complexity.
I have this algorithm written in Java:
String[] remove = value.split(",");
String[] temp = (pb.get(key)).split(",");
String newval = "";
for (int i=0;i<remove.length;i++){
for (int j=0;j<temp.length;j++){
if (remove[i].equals(temp[j])){
temp[j] = "";
}
}
}
for (String str : temp){
if (!(str.isEmpty())){
newval = newval + "," + str;
}
}
newval = newval.substring(1, newval.length());
pb.put(key, newval);
From my understanding, the first loop (nested loop) would have a time complexity of O(N^2) and the second loop would be O(N). Am I correct?
And how do I calculate for the space complexity ?
Also, an unrelated question; how do I change the first loop into an enhanced for-loop ?
Thanks in advance!