@codestorywithMIK

I have not been well since last night. Slight fever ๐Ÿค’.
Apologies for any quality issue in todayโ€™s video.
Hope you all understand โค๐Ÿ™
(Make sure to try this problem with Backtracking as well)

@tarupathak

You are the epitome of simplicity, thanks for the motivation and and for this goldmine disguised as video as well. THANKS AGAIN:)

@awalkingnosebleed

aapki aawaz bht relaxing hai sir ๐Ÿ˜ญ๐Ÿ™

@NitinKumar-gv4hs

Hi Mik, Thanks for putting up the editorials and explaination covering from brute to optimised approach, you're a gem.:face-red-heart-shape:

@gauravparasar4571

guruji mujhe esa feel hota hai ab potd ke videos banan apki duty hogai hai isse kabhi mt chhodna halanki har cheej hamesha to chalti nahi hai but fir bhii kuki ye ek esi duty hai jiske peeche hajaro bacche hai apke sahare se padh rhe hai question solve kr rhe hai or aage badh rhe hai
mere mann ki baat thi mene bol di 
thanks ๐Ÿ™๐Ÿ™๐Ÿ™๐Ÿ™

@aws_handles

Stack wala awesome solution hai.
Maine backtracking se bhi banaya ise

@annagarg2567

Hi ,
I don't know who should be really reading it , But whoEver is reacting on Bhaiya's post with that sign on WhatsApp, 
"SHAME ON YOU"


its been weeks i am seeing that reaction , And i Hate it everydayyyy, 
Today I am posting about it

@shaundabre5552

You will become unstoppable, once you know that every setback is very much a reason for your greatest comeback

@TanmayMankar-26

I reach pupil on codeforces thank you for your contribution bhaiya

@nehasingh-mc9hz

Your second approach is absolutely brilliant! Iโ€™m speechless

@dheemanthnj2075

Stack approach is too good !!๐Ÿคฉ

@gui-codes

Easily solve hogaya . Maine backtracking laga diya khandani template because constraint was very small. 
and Hats off to your dedication. take care MIK

@jain5184

Take a rest mik
Its okiay
U r so consistent taking break is good for health as well as for mental peace

@i.m.r.a.n_sk

Thank You ! loved your explanationโค

@Idk-uy3lk

Did it using backtracking but here for the most optimal way.Thanks MIK bhaiya.

@lakshyarajput5542

this problem's editorial is crazy.

@hemant_kumar_071

class Solution {
public:
    bool solve(int i, string &pattern, string &num, vector<bool> &used) {
        if (num.size() == pattern.size() + 1) return true;

        for (int j = 1; j <= 9; j++) {
            if (used[j]) continue;
            
            if ((pattern[i] == 'I' && num.back() - '0' < j) ||
                (pattern[i] == 'D' && num.back() - '0' > j)) {
                
                num += to_string(j);
                used[j] = true;

                if (solve(i + 1, pattern, num, used)) return true;
                
                used[j] = false;
                num.pop_back();
            }
        }
        return false;
    }

    string smallestNumber(string pattern) {
        vector<bool> used(10, false);

        for (int i = 1; i <= 9; i++) {
            string num = to_string(i);
            used[i] = true;

            if (solve(0, pattern, num, used)) return num;
            
            used[i] = false;
        }
        
        return "";
    }
};

@namansaini1073

First try mei run and accept hogya code. All thanks to the khandaani backtracking template ๐Ÿ™๐Ÿ’ช

@ayush46712

class Solution {
public:
    string smallestNumber(string pattern) {

        string result;
        stack<int> stk;
        
        // Iterate through the pattern and push elements to stack
        for (int i = 0; i <= pattern.size(); ++i) {
            stk.push(i + 1);  // Push the next number
            
            // If it's an 'I' or end of pattern, pop all from stack
            if (i == pattern.size() || pattern[i] == 'I') {
                while (!stk.empty()) {
                    result += to_string(stk.top());
                    stk.pop();
                }
            }
        }
        
        return result;
        
    }
};

@abhinay.k

thanks