Sunday, July 7, 2019

Prepending in StringBuilder in Java

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.

No comments:

Post a Comment