Bit manipulation solution java


  • 0
    Y
    public class Solution {
    public List<String> generateAbbreviations(String word) {
        List<String> result = new ArrayList<String>();
        if(word.length() == 0){
            result.add("");
            return result;
        }
        int n = word.length();
        int amount = (int)(Math.pow(2,n));
        for(int i = 0; i < amount; i++){
            int num = i;
            int bit = 0;
            StringBuilder temp = new StringBuilder();
            int count = 0;
            for(int j = 0; j < n; j++){
                bit = ( num >> j) & 1;
                if(bit == 0){
                        count++;
                    if(j == n - 1)
                        temp.append(count);
                }
                else
                {
                    if(count != 0){
                        temp.append(count);
                        count = 0;
                    }
                    temp.append(word.charAt(j));
                }
            }
            result.add(temp.toString());
        }
        return result;
    }
    

    }


Log in to reply
 

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