Open yokostan opened 5 years ago
https://leetcode.com/problems/generalized-abbreviation/discuss/77190/Java-backtracking-solution
class Solution { public List<String> generateAbbreviations(String word) { List<String> res = new ArrayList<>(); helper(res, word, 0, "", 0); return res; } private void helper(List<String> res, String word, int pos, String cur, int count) { if (pos == word.length()) { if (count > 0) cur += count; res.add(cur); } else { helper(res, word, pos + 1, cur, count + 1); helper(res, word, pos + 1, cur + (count > 0 ? count : "") + word.charAt(pos), 0); } } }
We can divide the case to 'abbreviate it' or 'omit it'.
https://leetcode.com/problems/generalized-abbreviation/discuss/77190/Java-backtracking-solution
We can divide the case to 'abbreviate it' or 'omit it'.