320. Generalized Abbreviation
Generalized Abbreviation
Example:Solution
public class Solution {
public List<String> generateAbbreviations(String word) {
List<String> result = new ArrayList<>();
helper(word, 0, result, "", false);
return result;
}
private void helper(String word, int index, List<String> result, String sb, boolean isNumPrev) {
if (word.length() == index) {
result.add(sb);
return;
}
if (!isNumPrev) {
for (int i = index + 1; i <= word.length(); i++) {
helper(word, i, result, sb + String.valueOf(i - index), true);
}
}
helper(word, index + 1, result, sb + word.charAt(index), false);
}
}Last updated