Google Interview Question

String manipulation with less time complexity