Below code can be used for prepending:- Time complexity O(n)
StringBuilder prepend(StringBuilder sb, String text){
sb.reverse();
sb.append(text);
sb.reverse();
return sb;
}
Using sb.insert(0, text) is not recommended.
StringBuilder prepend(StringBuilder sb, String text){
sb.reverse();
sb.append(text);
sb.reverse();
return sb;
}
Using sb.insert(0, text) is not recommended.
No comments:
Post a Comment