解法1:
class Solution {
public List<String> stringSequence(String target) {
List<String> ans = new ArrayList<>();
StringBuilder path = new StringBuilder();
path.append('a');
ans.add(path.toString());
int n = target.length();
for (int i = 0; i < n; i++) {
// 计算需要按按键2的次数 move
int move = target.charAt(i) - path.charAt(i);
// 将 path 的第 i 位依次更改为 target 的第 i 位
for (int j = 0; j < move; j++) {
char nxtChar = (char)(path.charAt(i) + 1);
path.setCharAt(i, nxtChar);
ans.add(path.toString());
}
// path 字符个数小于 target 字符个数就继续追加 'a'
if (i < n - 1) {
path.append('a');
ans.add(path.toString());
}
}
return ans;
}
}
解法2:将 target 转换为 char Array
class Solution {
public List<String> stringSequence(String target) {
List<String> ans = new ArrayList<>();
StringBuilder path = new StringBuilder();
for (char c : target.toCharArray()) {
path.append('a');
for (char i = 'a'; i <= c; i++) {
path.setCharAt(path.length() - 1, i);
ans.add(path.toString());
}
}
return ans;
}
}