classSolution(object): defsmallestString(self, s): """ :type s: str :rtype: str """ defcal(s): arr = [-1] n = len(s) for i inrange(n): if s[i] == "a": arr.append(i) iflen(arr) == 1: return"".join([chr(ord(c)-1) for c in s]) if arr[1] != 0: return"".join([chr(ord(s[i])-1) for i inrange(arr[1])]) + s[arr[1]:] cnt = 0 n = len(s) for i inrange(n): if s[i] == "a": cnt += 1 else: break if cnt != n: return"a"*cnt + cal(s[cnt:]) return"a" * (n-1) + "z"
classSolution(object): defsmallestString(self, s): """ :type s: str :rtype: str """ i = 0 n = len(s) s = list(s) while i < n and s[i] == 'a': i += 1 if i == n: s[-1] = 'z' while i < n and s[i] != 'a': s[i] = chr(ord(s[i]) - 1) i += 1 return''.join(s)