Solve a string manipulation or array-based problem using optimal time complexity, ensuring edge cases are handled.