320 Generalized Abbreviation
Problem:
Solutions:
public class Solution {
public List<String> generateAbbreviations(String word) {
List<String> result = new LinkedList<String>();
result.add(word);
for (int i = 1; i <= word.length(); i ++) {
for (int j = 0; j + i <= word.length(); j ++) {
List<String> following = generateAbbreviations(j + i + 1 <= word.length()?word.substring(j + i + 1):"");
for (String f:following) {
String s = word.substring(0, j) + i + (j + i < word.length()?word.charAt(j + i):"") + f;
result.add(s);
}
}
}
return result;
}
}Last updated