Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.9 kB
1
Indexable
Never
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

    private static final byte[][] KEYBOARD_MAPPING =  {
            {'a', 'b', 'c'},
            {'d', 'e', 'f'},
            {'g', 'h', 'i'},
            {'j', 'k', 'l'},
            {'m', 'n', 'o'},
            {'p', 'q', 'r', 's'},
            {'t', 'u', 'v'},
            {'w', 'x', 'y', 'z'}
    };

    public static List<String> solution(String text) {
        if (text.length() == 0)
            return Collections.emptyList();

        final int length = text.length();

        int[] digits = new int[text.length()];
        int count = 1;

        for (int i = 0; i < text.length(); i++) {
            final int digit = text.charAt(i) - '2';
            digits[i] = digit;

            count *= KEYBOARD_MAPPING[digit].length;
        }

        List<byte[]> words = new ArrayList<>(count);

        for (int i = 0; i < count; i++)
            words.add(new byte[length]);

        int skip = count;

        for (int i = 0; i < length; i++) {
            final int digit = digits[i];
            final byte[] letters = KEYBOARD_MAPPING[digit];
            final int width = letters.length;

            skip /= width;

            for (int j = 0, k = 0; j < count; j += skip, k = (k + 1) % width) {
                final byte letter = letters[k];

                for (int l = j; l < j + skip; l++) {
                    byte[] word = words.get(l);
                    word[i] = letter;
                }
            }
        }

        List<String> _words = new ArrayList<>(count);

        for (byte[] word : words)
            _words.add(new String(word, StandardCharsets.US_ASCII));

        return _words;
    }

    public static void main(String[] args) {
        System.out.println(solution("2").size());
    }
}