{ "id": 3540, "name": "hash_divided_string", "difficulty": "Medium", "link": "https://leetcode.com/problems/hash-divided-string/", "date": "2024-08-17 00:00:00", "task_description": "You are given a string `s` of length `n` and an integer `k`, where `n` is a **multiple** of `k`. Your task is to hash the string `s` into a new string called `result`, which has a length of `n / k`. First, divide `s` into `n / k` **substrings**, each with a length of `k`. Then, initialize `result` as an **empty** string. For each **substring** in order from the beginning: The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0`, `'b' → 1`, ..., `'z' → 25`). Calculate the _sum_ of all the **hash values** of the characters in the substring. Find the remainder of this sum when divided by 26, which is called `hashedChar`. Identify the character in the English lowercase alphabet that corresponds to `hashedChar`. Append that character to the end of `result`. Return `result`. **Example 1:** **Input:** s = \"abcd\", k = 2 **Output:** \"bf\" **Explanation:** First substring: `\"ab\"`, `0 + 1 = 1`, `1 % 26 = 1`, `result[0] = 'b'`. Second substring: `\"cd\"`, `2 + 3 = 5`, `5 % 26 = 5`, `result[1] = 'f'`. **Example 2:** **Input:** s = \"mxz\", k = 3 **Output:** \"i\" **Explanation:** The only substring: `\"mxz\"`, `12 + 23 + 25 = 60`, `60 % 26 = 8`, `result[0] = 'i'`. **Constraints:** `1 <= k <= 100` `k <= s.length <= 1000` `s.length` is divisible by `k`. `s` consists only of lowercase English letters.", "public_test_cases": [ { "label": "Example 1", "input": "s = \"abcd\", k = 2", "output": "\"bf\" " }, { "label": "Example 2", "input": "s = \"mxz\", k = 3", "output": "\"i\" " } ], "private_test_cases": [ { "input": [ "\"mknnewhsvixaeusxsfojlvplbqyjyefrjyyhtajuilrfkjghjnsvnqaydn\"", 58 ], "output": "h" }, { "input": [ "\"hkbsjdlmmpstbixdukm\"", 19 ], "output": "e" }, { "input": [ "\"hrenpeulegbdevkqxkgunlvgjcgcnkyfveviopkctwxwhbbkolfhagijdtrdizpirypifljngxpnptiieyomhqjjktmgoenqopchwgogggndissmbydubfvnydiiishhouqhtpyrzrfprmincrzitkspkqzrmqyendodxzqfoshzusltfmlryehtrbkjmfedsamvuitkilacsmmhbkvngiaaggirnphjxtfegigchuagijfyinrqcnqolrrtstnikodwzxkqgqvcodvgeyiyprivurjsfggblcdhutatqxcaszyplcjyxhvxjdjnbnmolpkqjfcjntszgitnmhnrxjpkpqzibqreaalrcdjxdzhgkotxgcngwtqkpsuxpqesdljpxflnczyajozieygfecszaxnkwwawzdacpermpwcivdviveknyjvlzyyjkedipifsiksmhofosobpjbxmjtxfheetuzozcwybccmdkyphebjngzmesjhxvgfcllrjsicgogubevwoyqclpkinjgmowkmqbbbjgnlruztuvssvpzexnticbbqnfprshbtbcomjsiwnnjuvvv\"", 59 ], "output": "mfghqouxbh" }, { "input": [ "\"qsxlpirfwsthwxzadtjsrzwzkcdbamjlkvimwhimffqwjjgmgaeygpxjheaahqqmtgooaybcybycgfvlimxuksbqbtqkddgussimvqoagtuvrjysyxjifasioqcmtsynguoiblpvqqxudxjcdokpbdkvxsqjextaiyubizlcjrdrwfsvivnmxcknskwkznvekfaileikjclsqsfjhmfxcphvdedxjznytvlpuhwigpixxycbbamgelxwwfhkpjsgqewpixzmpftsbaewaujcowqaqaulumntwxjysxcgwpezewkvddbsrymkwwyhkuguawfljwuaqsldbwuxxfbmtbrgrsvqlpvtsyeascudehuenihqah\"", 37 ], "output": "obknkshumu" }, { "input": [ "\"imdlgramytqqhnxvrssthdpmsymozgryvqfytyufmrhzctoqcntuyrsjswjeqy\"", 31 ], "output": "rr" }, { "input": [ "\"mrcfbffhqqxjcsicacqwxydlitlcacwbzvkbnpofpjztlptsigapmfowackpdvbscvbzvwcjeoewrtaxigvysjwxokurihlt\"", 48 ], "output": "dx" }, { "input": [ "\"mtztdwkprxmyslkvbzbctmdvkbgeqqvjtlelfxweovjijdckkxvftardiknyrvavdpvxafjbptmxzqlymyjjvrphubfbczwfhqdpvrvzvhuchzvyykdzzpxzxoffxhgzrvljihabhdpallqikkgkafwmlqtwsikhmrmamwswpxiinlncjuwndgxguhzuckqaukbodcinwublsvpftqwmpgaptfpajzsejcrpjechadcfxjdxmleklprvbgwcfjoriznbwryevhbyfdtaswzeqyogsgustciuedccxazekzgmoxaxbextrcktiphwjbtfffhslzxfishinywflldkozkkamtjpcwkuhcdshfmoadlfoalpvyfktmytetiwsscckeqpppuewghuhgangabrkqkwgibulxtyegiinfpbysbatidnxiqmlduqybpsftzzduyjemfyohwruxxettnfsfdliadoehlqwwhmvkcidpxkjxhhggtnctaczmilwsbaanyzhoktyhbqkpv\"", 66 ], "output": "bffewqcx" }, { "input": [ "\"tveaugfeecjmlzdskgaz\"", 5 ], "output": "mvih" }, { "input": [ "fwwlcqeiznjbsellbqoyydgqvghkxfpqwhbrceaapetqfjff", 24 ], "output": "aa" }, { "input": [ "lzrjjwaxglcicsujxldilsqdcazfiknweaavbyfpikbkvduavjxdpsnuuitabguyqhevqjjbqjhkbjhhndqwtflawguetmibvirgxomrkeoqufgbwwyzofxpphyumynhzvpqtnfuzsgbczcqoxasdrljnhytwgvjdzxdxqongohlqyztxuqaobhwdokrfkrjaktoeuovnpnyftjttydihvueifjhdmznimbqbvhsqjgqdxkpoogqanfakqlfxjuvsqxavytbbtvfhotridmdcxgbfniaqsdmbxsobicwmkamwljpaqpfjsglrwozobzwbavbpdliaqkbblqonvkglpkqhdbrgf", 70 ], "output": "axrnr" } ], "haskell_template": "stringHash :: String -> Int -> String\nstringHash s k ", "ocaml_template": "let stringHash (s: string) (k: int) : string = ", "scala_template": "def stringHash(s: String,k: Int): String = { \n \n}", "java_template": "class Solution {\n public String stringHash(String s, int k) {\n \n }\n}", "python_template": "class Solution(object):\n def stringHash(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: str\n \"\"\"\n " }