Word Break

2025. 2. 3. 13:28·PS/LeetCode
반응형

 

문제

https://leetcode.com/problems/word-break/description/

입력

1 <= s.length <= 300 // 주어진 문장 길이 
1 <= wordDict.length <= 1000 // 단어 개수
1 <= wordDict[i].length <= 20 // 단어 길이

결과

ans: Bool = wordDict를 이용해서 s를 만들 수 있는가

해석

cache[i] = i번째 단어에 도달할 수 있는가를 판단한다.

cache[i]가 true면 wordDict에 있는 단어를 하나씩 돌면서 맞는게 있는지 비교

코드

import Foundation

extension String {
    subscript(_ range: Range<Int>) -> String {
        let startIndex = self.startIndex
        let from = self.index(startIndex, offsetBy: range.startIndex)
        let to = self.index(startIndex, offsetBy: range.endIndex)
        return String(self[from..<to])
    }
}

class Solution {
    func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
        if s.isEmpty {
            return true 
        }
     
        let n = s.count 
        var cache: [Bool] = [Bool](repeating: false, count: n+1)
        cache[0] = true 
        
        for i in 0...n {
            if !cache[i] { continue }
            for word in wordDict {
                let len = word.count
                let j = i+len
                if j <= n && s[i..<j] == word {
                    cache[j] = true
                }
            }
        }

        return cache[n]
    }
}

 

반응형

'PS > LeetCode' 카테고리의 다른 글

House Robber  (0) 2025.02.03
Combination Sum IV  (2) 2025.02.03
300. Longest Increasing Subsequence  (0) 2025.02.02
Climbing Stairs  (1) 2025.02.02
Container With Most Water  (0) 2025.01.12
'PS/LeetCode' 카테고리의 다른 글
  • House Robber
  • Combination Sum IV
  • 300. Longest Increasing Subsequence
  • Climbing Stairs
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (304)
      • CS (30)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (6)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (37)
        • swift (32)
        • python (4)
        • Kotlin (1)
      • iOS (132)
        • UIKit (37)
        • Combine (1)
        • SwiftUI (32)
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (11)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (13)
        • 어노테이션 (1)
        • 튜토리얼 (11)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백준
    GIT
    dfs
    AVFoundation
    SwiftUI
    UIKit
    프로그래머스
    boostcamp
    Tuist
    concurrency
    dp
    lifecycle
    property
    Spring
    protocol
    CS
    Swift
    IOS
    dispatch
    투포인터
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
Word Break
상단으로

티스토리툴바