⚡Brute Force + 🔥Optimized(SlidingWindow) || ✅Clean Code
\n# Complexity\n\n- Time complexity:\nO(n ^ 2) for Brute Force Approach\nO(n)     for Optimized Approach\n\n- Space complexity:\nO(1)\n\n\n# Code\n## Please Upv
\n# Complexity\n```\n- Time complexity:\nO(n ^ 2) for Brute Force Approach\nO(n)     for Optimized Approach\n\n- Space complexity:\nO(1)\n```\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n# BRUTE FORCE :-\n```\nclass Solution {\npublic:\n    string shortestBeautifulSubstring(string s, int k) {\n        int n = s.size(), mini = 101;\n        string ans = "{";\n        for(int i = 0 ; i < n ; i++){\n            string g = "";\n            int cnt = 0;\n            for(int j = i ; j < n ; j++){\n                g += s[j];\n                if(s[j] == \'1\')\n                    cnt++;\n                if(cnt == k){\n                    if(g.size() <= mini){\n                        mini = g.size();\n                        if(g.size() == ans.size()){\n                            if(g < ans)\n                                ans = g;\n                        }\n                        else\n                            ans = g;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        return ans == "{" ? "" : ans;\n    }\n};\n```\n# OPTIMIZED APPROACH(Sliding Window) :-\n```\nclass Solution {\npublic:\n    string shortestBeautifulSubstring(string s, int k) {\n        int n = s.size(), mini = n, i = 0, j = 0, cnt = 0;\n        string ans = "a", tmp = "";\n        while(j < n){\n            if(s[j] == \'1\')\n                cnt++;\n            while(cnt == k){\n                tmp = s.substr(i, j - i + 1);\n                int size = tmp.size();\n                if(size <= mini){\n                    mini = size;\n                    if(size == ans.size()){\n                        if(tmp < ans)\n                            ans = tmp;\n                    }\n                    else\n                        ans = tmp;\n                }\n                if(s[i++] == \'1\')\n                    cnt--;\n            }\n            j++;\n        }\n        return ans == "a" ? "" : ans;;\n    }\n};\n```
['C++']