Generating unique ids with the max length of 3 digits/letters/simbols

前端 未结 3 1664
迷失自我
迷失自我 2021-01-17 08:12

I have a list of 75200 words. I need to give a \'unique\' id to each word, and the length of each id could be 3 letters or less. I can use numbers, letters or even symbols b

相关标签:
3条回答
  • 2021-01-17 08:20

    (Posted on behalf of the question author).

    This is how I wrote my code according to the answer of Stack Overflow user "Keppil".

    import java.io.*;
    import java.util.*;
    
    public class HashCreator 
    {
        private Map completedWordMap;
        private String[]simpleLetters = {"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"};
        private char[] A;
    
            private static final String[] symbols = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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 HashCreator()
        {
    
             for(int i=0;i<75001;i++)
             {
                System.out.println(getSequence(i));
             }
        }
    
    
    
    
        public static String getSequence(final int i) {
        return symbols[i / (symbols.length * symbols.length)] + symbols[(i / symbols.length) % symbols.length]
                + symbols[i % symbols.length];
    }
    }
    
    0 讨论(0)
  • 2021-01-17 08:23

    You could create a method that basically converts a decimal number to a base of your choice. Here I have 46 symbols for example, which gives 97336 unique sequences:

    private static final String[] symbols = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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 String getSequence(final int i) {
        return symbols[i / (symbols.length * symbols.length)] + symbols[(i / symbols.length) % symbols.length]
                + symbols[i % symbols.length];
    }
    
    0 讨论(0)
  • 2021-01-17 08:28

    Here is generator with enough IDs.

    public class Main {
        private char[] A;
        void init()
        {
           A = new char[] { 
               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
               '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',
               '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'
           };
           System.out.println("digits = " + A.length);
    
           //for (int i = 0; i < A.length; i++)
           //    System.out.print(A[i] + " ");
           //System.out.println();
        }
    
        public void generate(int length, String id)
        {
            if (length == 3) {
                System.out.println(id);
            } else {
                for (int i = 0; i < A.length; i++)
                    generate(length + 1, id + A[i]);
            }
        }
    
        public static void main(String[] args) {
            Main test = new Main();
            test.init();
            test.generate(0,  "");
        }
    }
    

    The number of unique IDs is (26 + 26 + 10) ^ 3 = 62^3 = 238328.

    Obviously you need to adapt it to fit your particular problem.

    Actually only 43 characters are needed since 43 ^ 3 = 79507 > 75200.

    EDIT: Explanation of the generate() method.

    This method implements a recursive algorithm to generate combinations of characters (the keys). The meaning of the parameters is the following:

    • length The length of the key.
    • id stores the combination of characters.

    The following picture can help to understand the algorithm.

    enter image description here

    This is similar to how the decimal (or any other base) numbers are formed.

    A thing that I don't noticed is that you are trying to first create all the possible keys of length 1, then all possible keys of length 2, and so on. My generator creates keys of exactly 3 character only. That behavior can be achieved modifying the generate() method as follows:

    public void generate(int count, String id)
    {
        if (count == 0) {
            System.out.println(id);
        } else {
            for (int i = 0; i < A.length; i++)
                generate(count - 1, id + A[i]);
        }
    }
    

    And then call the method tree times:

    test.generate(1,  "");
    test.generate(2,  "");
    test.generate(3,  "");
    

    Some keys contains leading zeros but that shouldn't be a problem since this keys are identifiers, not numbers. The number of possible keys increases by length(alphabet) + length(alphabet) ^ 2, i.e. we have 62 + 62^2 additional keys.

    Since the length of the key is at most 3 the iterative version can be easily implemented using for loops:

    public void iterative_generator()
    {
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < A.length; j++) {
                for (int k = 0; k < A.length; k++) {
                    System.out.println("" + A[i] + A[j] + A[k]);
                }
            }
        }
    }
    

    I think you get the idea.

    0 讨论(0)
提交回复
热议问题