We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Couple comments for this code. First and foremost, the problem was to test your knowledge of printf.
Otherwise looking just at your solution, instead of starting at j=0, you could start at s1.length() which would save you from the unnecessary loops where "if(s1.length() < 15)" is false. In fact, with the correcty initialized for loop, you could skip the if statement entirely.
Also, I wouldn't recommend building up your string in this way (printf aside) as the String is immutable and you're simply wasting memory. Java cannot optimize these appends as they occur on separate lines. You want StringBuilder here instead: http://stackoverflow.com/a/7817989/819373
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Output Formatting
You are viewing a single comment's thread. Return to all comments →
Couple comments for this code. First and foremost, the problem was to test your knowledge of printf.
Otherwise looking just at your solution, instead of starting at j=0, you could start at s1.length() which would save you from the unnecessary loops where "if(s1.length() < 15)" is false. In fact, with the correcty initialized for loop, you could skip the if statement entirely.
Also, I wouldn't recommend building up your string in this way (printf aside) as the String is immutable and you're simply wasting memory. Java cannot optimize these appends as they occur on separate lines. You want StringBuilder here instead: http://stackoverflow.com/a/7817989/819373