I did not have fun writing this in Java. Did you?


  • 0

    From this code I can see how AI and machine learning are going to replace programmers someday.

    public class Solution {
        // zero, one, two, three, four, five, six, seven, eight, nine
        //z, o, w, r, u, v, x, s, g, i
        //z, o - w,r,z, w, r - u,z, u, v - s, x, s - x, g, i - v,x,g
        //z, u, x, g, w, r - u,z, s - x, v - s, i - v,x,g, o - w,u,z
        
        /*
        add z, o, w, r, u, v, x, s to Map to value 0
        go through the string:
            if map.containsKey(c): map put c, get(c) + 1
        do all the arithmetics shown above
        map.get z: add 0's
        get o: add 1's
        ...
            
        */
        //owoztneoer
        //z1, o3, w1, r1
        //0123
        public String originalDigits(String s) {
            Map<Character, Integer> map = new HashMap<>();
            StringBuilder sb = new StringBuilder();
            map.put('z', 0);
            map.put('o', 0);
            map.put('w', 0);
            map.put('r', 0);
            map.put('u', 0);
            map.put('v', 0);
            map.put('x', 0);
            map.put('s', 0);
            map.put('g', 0);
            map.put('i', 0);
            for (char c : s.toCharArray()) 
                if (map.containsKey(c)) map.put(c, map.get(c) + 1);
            map.put('r', map.get('r') - map.get('u') - map.get('z'));
            map.put('s', map.get('s') - map.get('x'));
            map.put('v', map.get('v') - map.get('s'));
            map.put('i', map.get('i') - map.get('v') - map.get('x') - map.get('g'));
            map.put('o', map.get('o') - map.get('u') - map.get('w') - map.get('z'));
            for (int i = 0; i < map.get('z'); i += 1) sb.append(0);
            for (int i = 0; i < map.get('o'); i += 1) sb.append(1);
            for (int i = 0; i < map.get('w'); i += 1) sb.append(2);
            for (int i = 0; i < map.get('r'); i += 1) sb.append(3);
            for (int i = 0; i < map.get('u'); i += 1) sb.append(4);
            for (int i = 0; i < map.get('v'); i += 1) sb.append(5);
            for (int i = 0; i < map.get('x'); i += 1) sb.append(6);
            for (int i = 0; i < map.get('s'); i += 1) sb.append(7);
            for (int i = 0; i < map.get('g'); i += 1) sb.append(8);
            for (int i = 0; i < map.get('i'); i += 1) sb.append(9);
            return sb.toString();
        }
    }
    

Log in to reply
 

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