I have a question which says
Given an input
ababacad The output should be all the a's should come first together rest characters should follow their sequence as they were originally. i.e.
aaaabbcd I solved it like below code
String temp="", first="" ; for(int i=0;i<str.length;i++) { if(str.charAt(i)!='a') temp=temp+str.charAt(i); else first=first+str.charAt(i); } System.out.print(first+temp); The output matches but it says it is still not optimised. I guess its already order of N complexity. Can it be optimised further.
StringBuilderinstead of+on stringsStringBuilderinstead will optimize your solution.