saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 알고리즘
  • 취준
  • 릿코드
  • 딥러닝
  • Python
  • LeetCode
  • 백준
  • 리트코드
  • c++
  • 개발자
  • 알고리즘문제해결
  • 취업준비
  • two pointer
  • 온라인저지
  • 개발자 취업준비
  • BFS
  • 문제해결능력
  • 딕셔너리
  • DFS
  • 파이썬

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 1704. Determine if String Halves Are Alike

2022. 12. 2. 02:52

1704. Determine if String Halves Are Alike

Easy

You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.

Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.

Return true if a and b are alike. Otherwise, return false.

 

Example 1:

Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.

Example 2:

Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.

 

Constraints:

  • 2 <= s.length <= 1000
  • s.length is even.
  • s consists of uppercase and lowercase letters.

문제 풀이

  • 주어진 문장은 짝수의 길이를 가지고 있다. 
  • 이 문장을 반으로 나눴을때 대소문자 관계없이 모음(Vowel)의 개수가 같으면 True 아니면 False를 리턴한다.
  • 문제 제한을 보면 문장의 최대길이가 1000이며, O(N^2)로도 풀린다.
  • 문장전체를 소문자로 바꾼다.
  • Counter를 사용해 반을 나눈 문장의 알파벳의 개수를 센다.
  • 반으로 나뉜 두 문장에서 모음의 개수를 세어 답을 리턴한다.

소스 코드

class Solution:
    def halvesAreAlike(self, s: str) -> bool:
        s = s.lower()
        half_len = len(s)//2
        s1 = Counter(s[:half_len])
        s2 = Counter(s[half_len:])
        total1, total2 = 0, 0
        for l in ('a', 'e', 'i', 'o', 'u'):
            total1 += s1[l]
            total2 += s2[l]
        return total1 == total2
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 1165. Single-Row Keyboard  (0) 2022.12.02
[LeetCode] 1099. Two Sum Less Than K  (1) 2022.12.02
[LeetCode] 1207. Unique Number of Occurrences  (0) 2022.12.01
[LeetCode] 380. Insert Delete GetRandom O(1)  (0) 2022.11.30
[LeetCode] 253. Meeting Rooms II  (0) 2022.11.30
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 1165. Single-Row Keyboard
    • [LeetCode] 1099. Two Sum Less Than K
    • [LeetCode] 1207. Unique Number of Occurrences
    • [LeetCode] 380. Insert Delete GetRandom O(1)
    saurus2
    saurus2
    Simple is Best

    티스토리툴바