DataRepo's picture
Duplicate from FPEvalDataset/LeetCodeProblem
f7c0146 verified
{
"id": 2361,
"name": "calculate_digit_sum_of_a_string",
"difficulty": "Easy",
"link": "https://leetcode.com/problems/calculate-digit-sum-of-a-string/",
"date": "2022-04-10 00:00:00",
"task_description": "You are given a string `s` consisting of digits and an integer `k`. A **round** can be completed if the length of `s` is greater than `k`. In one round, do the following: **Divide** `s` into **consecutive groups** of size `k` such that the first `k` characters are in the first group, the next `k` characters are in the second group, and so on. **Note** that the size of the last group can be smaller than `k`. **Replace** each group of `s` with a string representing the sum of all its digits. For example, `\"346\"` is replaced with `\"13\"` because `3 + 4 + 6 = 13`. **Merge** consecutive groups together to form a new string. If the length of the string is greater than `k`, repeat from step `1`. Return `s` _after all rounds have been completed_. **Example 1:** ``` **Input:** s = \"11111222223\", k = 3 **Output:** \"135\" **Explanation:** - For the first round, we divide s into groups of size 3: \"111\", \"112\", \"222\", and \"23\". ​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5. So, s becomes \"3\" + \"4\" + \"6\" + \"5\" = \"3465\" after the first round. - For the second round, we divide s into \"346\" and \"5\". Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. So, s becomes \"13\" + \"5\" = \"135\" after second round. Now, s.length <= k, so we return \"135\" as the answer. ``` **Example 2:** ``` **Input:** s = \"00000000\", k = 3 **Output:** \"000\" **Explanation:** We divide s into \"000\", \"000\", and \"00\". Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. s becomes \"0\" + \"0\" + \"0\" = \"000\", whose length is equal to k, so we return \"000\". ``` **Constraints:** `1 <= s.length <= 100` `2 <= k <= 100` `s` consists of digits only.",
"public_test_cases": [
{
"label": "Example 1",
"input": "s = \"11111222223\", k = 3",
"output": "\"135\" "
},
{
"label": "Example 2",
"input": "s = \"00000000\", k = 3",
"output": "\"000\" "
}
],
"private_test_cases": [
{
"input": [
"\"0\"",
2
],
"output": "0"
},
{
"input": [
"\"6421145198777362190042394610980120758216614468524308044135603727111584866360410865574469873746173533\"",
2
],
"output": "59"
},
{
"input": [
"\"6828549665923521139586766197204369524222360091033439842730341293447277906510091763414462467630345859\"",
100
],
"output": "6828549665923521139586766197204369524222360091033439842730341293447277906510091763414462467630345859"
},
{
"input": [
"\"86835675433222437140367269363341651026820150582311446699662136\"",
12
],
"output": "60414742559"
},
{
"input": [
"\"09556959624863822113055328888211114768861024488160715110957533476207255541787\"",
52
],
"output": "228109"
},
{
"input": [
"\"33628763334261975447998353546830413109540456\"",
40
],
"output": "18315"
},
{
"input": [
"\"4261628821970427731371304870116953\"",
26
],
"output": "10632"
},
{
"input": [
"\"6260198388\"",
7
],
"output": "3219"
},
{
"input": [
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
30
],
"output": "000"
},
{
"input": [
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
100
],
"output": "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
}
],
"haskell_template": "digitSum :: String -> Int -> String\ndigitSum s k ",
"ocaml_template": "let digitSum (s: string) (k: int) : string = ",
"scala_template": "def digitSum(s: String,k: Int): String = { \n \n}",
"java_template": "class Solution {\n public String digitSum(String s, int k) {\n \n }\n}",
"python_template": "class Solution(object):\n def digitSum(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: str\n \"\"\"\n "
}