지난 공부기록/알고리즘 풀이

Python - Algorithm #48

hkl22 2024. 9. 6. 09:34

Python Algorithm

LeetCode

2586. Count the Number of Vowel Strings in Range

  • You are given a 0-indexed array of string [words] and two integers left and right.
  • A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'.
  • Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].
  • Constraints
    • 1 words.length 1000
    • 1 words[i].length 10
    • words[i] consists of only lowercase English letters.
    • 0 left right < words.length
class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        # for, in
        vowels = ["a", 'e', 'i', 'o', 'u']
        answer = 0
        for i in range(left, right + 1):
            word = words[i]
            # word의 각 글자를 반복하면서 모음인지 체크하고
            # 모음이면 answer에 1 더해주기
            if word[0] in vowels and word[-1] in vowels:
                answer += 1
        return answer
class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        answer = 0
        vowels = "aeiou"
        for i in range(left, right+1):
            if words[i][0] in vowels and words[i][-1] in vowels:
                answer += 1
        return answer

1309. Decrypt String from Alphabet to Integer Mapping

  • You are given a string s formed by digits and '#'. We want to map s to English lowercase characters as follows:
    • Characters ('a' to 'i') are represented by ('1' to '9') respectively.
    • Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.
  • Return the string formed after mapping.
  • The test cases are generated so that a unique mapping will always exist.
  • Constraints
    • 1 s.length 1000
    • s consists of digits and the '#' letter.
    • s will be a valid string such that mapping is always possible.
class Solution:
    def freqAlphabets(self, s: str) -> str:
        # 각 알파벳과 숫자 표기를 매핑한 딕셔너리 만들기
        d = {}
        for i in range(26):
            alphabet = chr(i + 97)
            num = i + 1
            if num < 10:
                d[str(num)] = alphabet
            else:
                d[str(num) + "#"] = alphabet
        sorted_keys = sorted(d.keys(), reverse=True, key=lambda x: (len(x), x))
        # 반복문 사용해서 앞에서부터 문자열 매칭해서
        # 일치하는 숫자부터 문자열에서 제거하기
        answer = ""
        while s:
            for key in sorted_keys:
                if s.startswith(key):
                    answer += d[key]
                    s = s[len(key):]
                    break
        return answer
class Solution:
    def freqAlphabets(self, s: str) -> str:
        d = {}
        for i in range(1, 27):
            if i < 10:
                d[str(i)] = chr(i+96)
            else:
                d[str(i) + "#"] = chr(i+96)
        sorted_d = sorted(d.keys(), reverse=True, key=lambda x: len(x))
        answer = ""
        while s:
            for key in sorted_d:
                if s.startswith(key):
                    answer += d[key]
                    s = s[len(key):]
                    break
        return answer
class Solution:
    def freqAlphabets(self, s: str) -> str:
        answer = ""
        i = 0
        while i < len(s):
            if i + 2 < len(s) and s[i+2] == "#":
                answer += chr(int(s[i:i+2]) + ord("a") - 1)
                i += 3
            else:
                answer += chr(int(s[i]) + ord("a") - 1)
                i += 1
        return answer

https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/

https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/

'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글

Python - Algorithm #50  (0) 2024.09.10
Python - Algorithm #49  (0) 2024.09.09
Python - Algorithm #47  (0) 2024.09.05
Python - Algorithm #46  (0) 2024.09.04
Python - Algorithm #45  (0) 2024.09.03