Sum of Digits of String After Convert

🏠 ⬅️ ➑️

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 (i.e., replace 'a' with 1, 'b' with 2, ..., 'z' with 26). Then, transform the integer by replacing it with the sum of its digits. Repeat the transform operation k times in total.

For example, if s = "zbax " and k = 2, then the resulting integer would be 8 by the following operations:

  • Convert: "zbax " ➝ "(26)(2)(1)(24) " ➝ "262124 " ➝ 262124
  • Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
  • Transform #2: 17 ➝ 1 + 7 ➝ 8

Return the resulting integer after performing the operations described above.

Example 1:

Input: s = "iiii ", k = 1 Output: 36 Explanation: The operations are as follows: - Convert: "iiii " ➝ "(9)(9)(9)(9) " ➝ "9999 " ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36.

Example 2:

Input: s = "leetcode ", k = 2 Output: 6 Explanation: The operations are as follows: - Convert: "leetcode " ➝ "(12)(5)(5)(20)(3)(15)(4)(5) " ➝ "12552031545 " ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6.

Example 3:

Input: s = "zbax ", k = 2 Output: 8

Constraints:

  • 1 <= s.length <= 100
  • 1 <= k <= 10
  • s consists of lowercase English letters.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=*), parameter :: s = "zbax "
    integer, parameter :: k = 2
    integer :: result

    result = solve(s, k)
    print '(I0)', result

contains

    function solve(s, k) result(result)
        implicit none
        character(len=*), intent(in) :: s
        integer, intent(in) :: k
        integer :: result

        ! convert s into an integer by replacing each letter with its position in the alphabet
        result = 0
        do i = 1, len_trim(s)
            result = result * 26 + ichar(s(i:i)) - ichar('a') + 1
        end do

        ! transform the integer by replacing it with the sum of its digits
        do j = 1, k
            result = sum(digits(result))
        end do

    contains

        function digits(n) result(d)
            implicit none
            integer, intent(in) :: n
            integer :: d(10)

            d = 0
            do while (n > 0)
                d(mod(n, 10) + 1) = mod(n, 10)
                n = n / 10
            end do
        end function digits

    end function solve

end program main
❌ Compiled
❌ Executed
❌ Correct
PROGRAM main

IMPLICIT NONE

CHARACTER(len=100) :: s
INTEGER :: k
INTEGER :: i
INTEGER :: result

! Read input
READ(*,*) s, k

! Convert string to integer
result = 0
DO i = 1, LEN(s)
    result = result * 26 + ICHAR(s(i:i)) - ICHAR('a') + 1
END DO

! Perform transformations
DO i = 1, k
    result = SUM(DIGITS(result))
END DO

! Output result
WRITE(*,*) result

END PROGRAM main
🌐 Data from online sources
from collections import defaultdict

def finding_users_active_minutes(logs, k):
    user_minutes = defaultdict(set)
    for log in logs:
        user_minutes[log[0]].add(log[1])
    result = [0] * k
    for minutes in user_minutes.values():
        if len(minutes) <= k:
            result[len(minutes) - 1] += 1
    return result
1. Create a data structure to store the unique minutes for each user ID. In this case, we use a hash map with user IDs as keys and sets of minutes as values.
  1. Iterate through each log entry, adding the minute of the action to the user's set of minutes in the data structure.
  2. Initialize an array of size k to store the final result.
  3. Iterate through the values of the data structure, which represent sets of unique minutes for each user. If the number of unique minutes for a user is less than or equal to k, increment the corresponding value in the result array.
  4. Return the result array.
🌐 Data from online sources
#include <vector>
#include <unordered_map>
#include <unordered_set>

std::vector<int> finding_users_active_minutes(std::vector<std::vector<int>>& logs, int k) {
    std::unordered_map<int, std::unordered_set<int>> user_minutes;
    for (const auto& log : logs) {
        user_minutes[log[0]].insert(log[1]);
    }
    std::vector<int> result(k, 0);
    for (const auto& entry : user_minutes) {
        if (entry.second.size() <= k) {
            result[entry.second.size() - 1]++;
        }
    }
    return result;
}
1. Create a data structure to store the unique minutes for each user ID. In this case, we use a hash map with user IDs as keys and sets of minutes as values.
  1. Iterate through each log entry, adding the minute of the action to the user's set of minutes in the data structure.
  2. Initialize an array of size k to store the final result.
  3. Iterate through the values of the data structure, which represent sets of unique minutes for each user. If the number of unique minutes for a user is less than or equal to k, increment the corresponding value in the result array.
  4. Return the result array.