Skip to content

Commit

Permalink
June27: Lexi smallest string after substrings ops [M]
Browse files Browse the repository at this point in the history
String manipulation of time O(N), and in-place update of O(1)
  • Loading branch information
aucker committed Jun 27, 2024
1 parent 1747fdb commit e7807a8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions daily/June27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief LC: 2734: Lexicographically Smallest string after substring ops
* Time: O(N), Space: O(1), in-place update s of O(1) space
*
* @param s
* @return string
*/
string smallestString(string s) {
int n = s.length();
for (int i = 0; i < n; i++) {
if (s[i] > 'a') {
// forward
for (; i < n && s[i] > 'a'; i++) {
s[i]--;
}
return s;
}
}
// all letter is 'a'
s.back() = 'z';
return s;
}
};

0 comments on commit e7807a8

Please sign in to comment.