Unknown's avatar

1281. Subtract the Product and Sum of Digits of an Integer

1281. Subtract the Product and Sum of Digits of an Integer

class Solution {
    public int subtractProductAndSum(int n) {
    
        ArrayList<Integer> list = new ArrayList<Integer>();
        while(n != 0){
        list.add(n%10);
        n = n/10;
    }
    int product = 1;
    int sum = 0;
    for(int i = 0; i < list.size(); i++){
        product *= list.get(i);
        sum+= list.get(i);
    }
    return product - sum;
}
}
Unknown's avatar

917. Reverse Only Letters

917. Reverse Only Letters

class Solution {
    public String reverseOnlyLetters(String s) {
        char[] arr = s.toCharArray();
            int arrIndex = getNextEmpty(arr.length,arr);

        for(int i = 0 ; i < s.length(); i++){
                char c = s.charAt(i);
                if((c>='a' && c <='z') ||
                    (c>='A' && c <='Z')){
                    arr[arrIndex] = c;
                    arrIndex = getNextEmpty(arrIndex,arr);
                    }
        }
        return new String(arr);
    }
    public int getNextEmpty(int current, char[] arr){
        for(int i = current-1; i >=0; i--){
            char c = arr[i];
            if((c >='a' && c <='z') ||
                    (c>='A' && c <='Z')) return i;
        }
        return 0;
    }
}