Coding/Basic Skills(Python)

[Programmers, Level 1] Weird Keyboard

xeohyuni 2023. 6. 5. 21:49

Description:

Unlike computer keyboards, cell phone keyboards can have multiple characters assigned to a single key. If multiple characters are assigned to one key, pressing the same key quickly in succession changes the characters in the order they are assigned.

For example, if key 1 is assigned characters in the order of "A", "B", and "C", then key 1 is "A", double is "B", and three is "C".

There is a cell phone keyboard that is randomly made by applying the same rules. The cell phone keyboard can have from one key to up to 100 keys, and characters entered when a certain key is pressed are also randomly arranged. In addition, the same character may be assigned multiple times throughout the keyboard, and in some cases, the same character may be assigned multiple times per key. There are even cases where it is not assigned at all. Therefore, some strings may not be able to be created.

When I write a specific string using this cell phone keyboard, I want to find out how many times I have to press the key at least to create that string.

When you are given a string array keymap containing the assigned characters in order from key 1, and a string array targets containing the characters you want to enter, complete the solution function that returns the key in order to write each string.

However, if the target string cannot be created, save -1.

Restriction:

  • 1 ≤ keymap length ≤ 100
    • The length of an element of 1 ≤ keymap ≤ 100
    • Keymap[i] means the characters that change in order when you press the i + 1 key.
    • For example, keymap[0] = "ABACD", press one key to get A, press two to get B, press three to get A.
    • The length of the elements in the keymap can be different.
    • The elements in the keymap are only capitalized alphabetic.
  • Length of 1 ≤ targets ≤ 100
    • The length of an element of 1 ≤ targets ≤ 100
    • The elements of targets are only capitalized letters.

Input and Output example:

Keymap Targets  Result
["ABACD", "BCEFD"] ["ABCD","AABB"] [9, 4]
["AA"] ["B"] [-1]
["AGZ", "BSSS"] ["ASA","BGZ"] [4, 6]

Explanation:

  • Input/Output Example #1
    • For "ABCD",
    • Number 1, key once → A
    • Number 2, key once → B
    • Number 2 key twice → C
    • 1 key 5 times → D
    • Therefore, we store the sum of 9 in the first index.
    • For "AABB",
    • Number 1, key once → A
    • Number 1, key once → A
    • Number 2, key once → B
    • Number 2, key once → B
    • Therefore, we store the sum of 4 in the second index.
    • As a result, return [9,4].
  • Input/Output Example #2
    • For "B", save -1 to the first index because 'B' is nowhere to be found.
    • As a result, return [-1].
  • Input/Output Example #3
    • For "ASA",
      Number 1, key once → A
      Number 2 key twice → S
      Number 1, key once → A
    • Therefore, we store the sum of 4 in the first index.
    • For "BGZ",
    • Number 2, key once → B
    • 1 key twice → G
    • 1 key 3 times → Z
    • Therefore, we store the total of 6 in the second index.
    • As a result, return [4, 6].

Code:

def solution(keymap, targets):
    ans  = []
    key_dict = {}
    for i in range(len(keymap)):
        for j in range(len(keymap[i])):
            if keymap[i][j] not in key_dict:
                key_dict[keymap[i][j]] = j+1
            else:
                if key_dict[keymap[i][j]] > j+1:
                    key_dict[keymap[i][j]] = j+1
    for target in targets:
        cnt = 0
        for t in target:
            if t in key_dict:
                cnt += key_dict[t]
            else:
                cnt = -1
                break
        ans.append(cnt)
   
    return ans

Discussion by using input/ourput #1:

It was important to solve this problem by understanding the use of element, key, and the value in for and dictionary in the fucntion. 

In the above section, the code creates a dictionary key_dict to store the mapping of characters in the keymap to their positions. It iterates over each character in the keymap using nested loops. If a character is encountered for the first time, it is added to the key_dict with its position (column index + 1). If the character already exists in the dictionary, the code checks if the current position is smaller than the previously stored position. If it is, the position in the dictionary is updated with the smaller value.

In the above section, the code iterates over each target string in the targets list. For each target string, it initializes a count cnt to 0. It then iterates over each character t in the target string. If the character t exists in the key_dict, the position of the character is added to the count cnt. If the character doesn't exist in the key_dict, the count cnt is set to -1, indicating that the target string cannot be mapped using the keymap. After processing all the characters in the target string, the count cnt is appended to the ans list.

Finally, the code returns the ans list, which contains the scores for each target string based on the positions of the characters in the keymap.
For the given input ["ABACD", "BCEFD"] as the keymap and ["ABCD","AABB"] as the targets, the output will be [9, 4]. This means that the first target string "ABCD" has a score of 9 (A:1 + B:2 + C:3 + D:3), and the second target string "AABB" has a score of 4 (A:1 + A:1 + B:2 + B:2).