LeetCode #2375

Construct Smallest Number From DI String

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    string smallestNumber(string pattern) {
        int N = (int)pattern.size() + 1;
        vector<int> V(N); iota(V.begin(), V.end(), 1);
        do {
            bool flag = true;
            for(int i = 1; flag && i < N; ++i) {
                char ch = 'D';
                if(V[i - 1] < V[i]) ch = 'I';
                if(pattern[i - 1] != ch) flag = false;
            }
            if(flag) break;
        } while(next_permutation(V.begin(), V.end()));
        string ans = "";
        for(int x: V) ans += to_string(x);
        return ans;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.