Share my concise java solution


  • 0
    public class Solution {
        public List<String> generateAbbreviations(String word) {
           List<String> result = new ArrayList<String>();
        if(word == null) {
          return result;
        }
        char[] arr = word.toCharArray();
        StringBuilder sb = new StringBuilder();
        helper(arr, sb, 0, 1, result);
        return result;
      }
      
      private void helper(char[] arr, StringBuilder sb, int index, int count, List<String> result) {
        if(index == arr.length) {  
          result.add(sb.toString());
          return;
        }
        int length = sb.length();
        sb.append(arr[index]);
        helper(arr, sb, index + 1, 1, result);
        sb.deleteCharAt(sb.length() - 1);
     
        while(sb.length() > 0 && Character.isDigit(sb.charAt(sb.length() - 1))) { 
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(count);
        helper(arr, sb, index + 1, count + 1, result);
        sb.setLength(length);
      }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.