aboutsummaryrefslogtreecommitdiff
path: root/sum_digit_str_convert.c
blob: d3a74be56742eaf4e4ce0f73b60f0b7cb1ef6bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 1945. Sum of Digits of String After Convert
#include "leetcode.h"

/*
 * you are given a string 's' consisting of lowercase english letters, and an
 * integer 'k'. first, convert 's' into an integer by replacing each letter with
 * its position in the alphabet (ie. replace 'a' with 1, 'b' with 2, and so on).
 * then, transform the integer by replacing it with the sum of its digits.
 * repeat the transform operation 'k' times in total. return the resulting
 * integer after performing the operations described above
 */

int transform(int sum, int k) {
  int res;
  for (int i = 1; i < k; i++) {
    res = 0;
    while (sum) {
      int m = sum % 10;
      res += m;
      sum /= 10;
    }
    sum = res;
  }
  return res;
}

int getLucky(char *s, int k) {
  int n = strlen(s), arr[n], ans = 0, pos = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    char curr = s[i];
    pos = curr - 96;
    arr[i] = pos;
  }
  for (int i = 0; i < n; i++) {
    if (arr[i] <= 9)
      sum += arr[i];
    else {
      int x = arr[i] % 10, y = arr[i] / 10;
      sum += (x + y);
    }
  }
  ans = k == 1 ? sum : transform(sum, k);
  return ans;
}

int main() {
  char *s1 = "iiii", *s2 = "leetcode", *s3 = "zbax";
  printf("%d\n", getLucky(s1, 1)); // expect: 36
  printf("%d\n", getLucky(s2, 2)); // expect: 6
  printf("%d\n", getLucky(s3, 2)); // expect: 8
}