Implemented next permutation of a string with time complexity as O(n), where n is the length of the input string.