public String getPermutation(int n, int k) {
        int total = factorial(n);
        StringBuffer candidate = new StringBuffer("123456789");
        candidate.substring(0, n);
        StringBuffer result = new StringBuffer();

        // count every bit
        for (int i = 0; i < n; i++) {
            total /= (n - i);
            int index = (k - 1) / total;
            result.append(candidate.charAt(index));
            candidate.deleteCharAt(index);
            k -= index * total;
        }
        return result.toString();
    }