0

If all the elements of the words array are all Strings of fixed length x,

public String joinWords(String[] words) {
   String sentence = "";
   for (String w : words) {
        sentence = sentence + w; 
   }
   return sentence;
}

Can someone explain to me how the runtime of this algorithm becomes O(xn^2) ? And how does StringBuilder or StringBuffer improve on this ?

Jenna Maiz
  • 792
  • 4
  • 17
  • 38
  • isn't the point of doing homework that you are supposed to figure that our for your self? – jtahlborn Jan 19 '16 at 04:32
  • if you research how string concatenation works, then you should be able to figure out the big-o easily. and there are plenty of examples out there which describe how to use StringBuilder to improve string concat in java. – jtahlborn Jan 19 '16 at 04:34
  • @jtahlborn I am actually preparing for an interview and was hoping someone can clarify( don't believe my username, I took Bayesian Statistics a while ago).... – Jenna Maiz Jan 19 '16 at 04:38
  • 1
    Although this is certainly a duplicate question, I feel I would be remiss if I neglected to direct anyone to this blog post. http://www.joelonsoftware.com/articles/fog0000000319.html – ddsnowboard Jan 19 '16 at 04:38

0 Answers0