LeetCode 풀기 - 139. Word Break

백지우·2026년 9월 21일

LeetCode

목록 보기
66/76
  • LeetCode 최소 하루에 하나는 풀자는 마인드
  • 사용언어: JAVA

139. Word Break

풀이

  • 방법: dp를 문자열의 길이+1의 길이를 가지며, 해당 index까지의 문자열이 wordDict안에 있는지 찾는 코드
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length()+1];
        dp[0] = true;

        for(int i = 1; i <= s.length(); i++){
            for (int j = 0; j < i; j++) {
                if(dp[j]){
                    dp[i] |= wordDict.contains(s.substring(j, i));
                }

            }
        }
        return dp[s.length()];
    }
}

0개의 댓글