Skip to content

Commit c9e1516

Browse files
committed
Added Lexicographically Smallest String After Substring Operation.java
1 parent 220e3c1 commit c9e1516

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public String smallestString(String s) {
3+
StringBuilder sb = new StringBuilder();
4+
int idx = 0;
5+
while (idx < s.length() && s.charAt(idx) == 'a') {
6+
sb.append(s.charAt(idx++));
7+
}
8+
if (idx == s.length()) {
9+
return s.substring(0, s.length() - 1) + 'z';
10+
}
11+
while (idx < s.length() && s.charAt(idx) != 'a') {
12+
sb.append(getPreviousChar(s.charAt(idx++)));
13+
}
14+
return sb.append(s.substring(idx)).toString();
15+
}
16+
17+
private char getPreviousChar(char c) {
18+
return (char) (c - 1);
19+
}
20+
}

0 commit comments

Comments
 (0)