Problem Β· String

URL Hashing 🐩

● EasyTiktokINTERNOA
See Tiktok hiring insights

Implement an algorithm to hash a URL as described.

Suppose the given URL url of length n is to be hashed with a string hash_string of length m. Given an integer k, run the url through the following algorithm:

  • Divide the URL into blocks of size k starting from the left. The last block can be smaller than k. For example, if url = "https://xyz.com" and k = 4, the blocks are ["http", "s://", "xyz.", "com"].
  • The values of the English characters 'a', 'b', ..., 'z' are 0, 1, ..., 25 respectively, and that of ':', '/' and '.' are 26, 27, and 28 respectively. Thus the has value of the block "s://" will be 19 + 26 + 27 + 27 = 98.
  • For each URL, find the hash value of each block. The hash value is the sum of the values of each character.
  • Replace the block with the (hash value of the block modulo m)th character of the string hash_string.
  • Given the string url, hash_string, and an integer k, find the hashed string.

    Examples
    01 Β· Example 1
    url = "https://xyz.com"
    hashString = "pqrst"
    k = 4
    return = "psps"
    Example 1 illustration
    Each hash value is divided by the len of hash_strong or 5 in this case. The remainders oint to the characters in hash_string, and the answer is "psps".
    Constraints
    Unknwo for now 🐼
    More Tiktok problems
    drafts saved locally
    public String hashUrl(String url, String hashString, int k) {
      // write your code here
    }
    
    url"https://xyz.com"
    hashString"pqrst"
    k4
    expected"psps"
    sign in to submit