However, you will realize that your proposed algorithm fails if your ID is a prime and greater than 52. I would continue your "convert number to string" approach. Do you think this is a good approach? Do you have a better idea?ĭue to the ongoing interest in this topic, I've published an efficient solution to GitHub, with implementations for JavaScript, PHP, Python and Java. That could be repeated until the number isn't divisible any more. If divisible by 3, add "b"+the result to short If divisible by 2, add "a"+the result to short Then I do the following steps: short = '' A self-built algorithm will work, too.įor " " I get the auto-increment id 239472. But what sort of hash should I build? Hash algorithms like MD5 create too long strings. This hash should then be inserted as " short". Then I would select the auto-increment value for " id" and build a hash of it. I would then insert the long URL into the table. short, string, the shortened URL (or just the six characters).long, string, the long URL the user entered.I have a database table with three columns: That makes 56~57 billion possible strings. Instead of " abcdef" there can be any other string with six characters containing a-z, A-Z and 0-9. I want to create a URL shortener service where you can write a long URL into an input field and the service shortens the URL to " ". It is not currently accepting new answers or interactions. This question and its answers are locked because the question is off-topic but has historical significance.
0 Comments
Leave a Reply. |