Google interview question

String manipulation with less time complexity