Split a String Into the Max Number of Unique Substrings

Split a String Into the Max Number of Unique Substrings

 

Account Login - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

- 문제 내용 : 주어진 string을 중복되지 않는 subset으로 구성 하였을 때 가장 긴 구성의 갯수는?

- 접근 방법 : dfs를 통해서 만들 수 있는 모든 경우의 수를 순환 했다.

단, Set을 이용해서 이미 만들어져 있는 subset이 다시 나타나는 경우에는 처리하지 않는 방식으로 접근 하면 된다.

var maxUniqueSplit = function(s, set) {
    let resultSet = dfs(s, new Set());
    return resultSet.size;
};

function dfs(s, set) {
    // console.log(set);
    if(s == '') return set;
    let result = new Set();
    for (let i = 1; i <= s.length; i++) {
        let left = s.substring(0, i);
        let right = s.substring(i);

        let nSet = new Set(set);

        if(nSet.has(left)) continue;
        nSet.add(left);

        let nnSet = dfs(right, nSet);
        if(result.size < nnSet.size){
            result = nnSet;
        }
    }
    return result;
}
728x90
반응형

'Problem Solving' 카테고리의 다른 글

Minimum Cost to Connect Two Groups of Points  (0) 2020.09.23
Maximum Non Negative Product in a Matrix  (0) 2020.09.20
Rearrange Spaces Between Words  (0) 2020.09.20
Sequential Digits  (0) 2020.09.19
Inorder Successor in BST II  (0) 2020.09.19