베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • 알고리즘
    • Spring
    • 코드업
    • 토비스프링
    • C++
    • 자바
    • dfs
    • leetcode
    • 스프링시큐리티
    • 스프링
    • BFS
    • 데이터베이스
    • 스레드
    • 이펙티브자바
    • 함수형인터페이스
    • 운영체제
    • 백준
    • jpa
    • java
    • 자바8

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Word Break (Java)

    2023. 7. 16. 21:28

    ✉️문제

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

     

    Word Break - LeetCode

    Can you solve this real interview question? Word Break - Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words. Note that the same word in the dictionary may

    leetcode.com

     

    📝 접근

    기본적인 아이디어는 wordDict안에 들어있는 word의 길이 만큼 문자열 s를 substring 한 뒤에 비교한다. 

    이때, 문자열 s의 알파벳이 모두 분리가 되는지 확인하기 위해 1차원 배열을 사용한다. 

     

    🗝 문제풀이

    class Solution {
        public boolean wordBreak(String s, List<String> wordDict) {
            int n = s.length();
            boolean[] dy = new boolean[n + 1];
            dy[n] = true;
    
            for(int i = n - 1; i >= 0; i--) {
                for(String word : wordDict) {
                    if(i + word.length() <= n && word.equals(s.substring(i, i + word.length()))) {
                        dy[i] = dy[i + word.length()];
                    }
    
                    if(dy[i]) break;
                }
            }
    
            return dy[0];
        }
    }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] House Robber II (Java)
      • [LeetCode] Combination Sum IV (Java)
      • [LeetCode] Longest Common Subsequence (Java)
      • [LeetCode] Longest Increasing Subsequence (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바