Post

문제풀이 - 1880. Check if Word Equals Summation of Two Words




문제

image alt 문제

https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/

1. 조건

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8

  • firstWord, secondWord, and targetWord consist of lowercase English letters from ‘a’ to ‘j’ inclusive.

2. 접근

  • ascii 코드를 이용해서 숫자로 치환할 수 있을 것

  • 전체를 모두 치환한 다음에 비교하기보다, 뒷자리 수부터 셈을 해나가면서 값이 맞는지 확인해가면 어떨지 —근데 이렇게 구현하는게 시간이 좀 더 걸릴 테니 쉽게 가자.

3. 제출한 답

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
        return (self._change_char_to_integer(firstWord) +
                self._change_char_to_integer(secondWord) ==
                self._change_char_to_integer(targetWord))

    def _change_char_to_integer(self, word: str) -> int:
        result: str = ''
        for char in word:
            result += str(ord(char) - ord('a'))
        return int(result)

시간복잡도

각각 단어의 길이를 n, m, o라고 할 때 O(n+m+o) 라고 볼 수 있다. 이는 O(n)이다.

4. 문자를 숫자로 변경하는 과정 수정해보기

1
2
3
4
5
6
 def _change_char_to_integer(self, word: str) -> int:
    result = 0
    for char in word:
        result = result * 10 + (ord(char) - ord('a'))
    return result

기존에는 문자열 연산으로 했으나, 이렇게 계산하면 str->int의 과정을 거치지 않고 바로 int로 만들 수 있다.

앞의 자리에서부터 계산하므로, 뒤 문자로 갈 수록 10을 곱해주는 방식.

This post is licensed under CC BY 4.0 by the author.