✅ O(N) | 100% 1ms | JAVA Simple solution ✅
\n# Complexity\n- Time complexity:\n  O(N) We iterate over the string of length N once.\n\nFOR BEST UNDERSTANDING OF CODE, DO DRY RUN ON YOUR OWN.\n\n# Code\n\n
\n# Complexity\n- Time complexity:\n  O(N) We iterate over the string of length N once.\n\nFOR BEST UNDERSTANDING OF CODE, DO DRY RUN ON YOUR OWN.\n\n# Code\n```\nclass Solution {\n    public String shortestBeautifulSubstring(String s, int k) {\n        int i = 0;\n        int c = 0;\n        int minLen = Integer.MAX_VALUE;\n        String result = "";\n\n        for (int j = 0; j < s.length(); j++) {\n            if (s.charAt(j) == \'1\') {\n                c++;\n            }\n\n            while (c == k) {\n                String s1 = s.substring(i, j + 1);\n                if (j - i + 1 < minLen) {\n                    minLen = j - i + 1;\n                    result = s1;\n                }else if(s1.length() == result.length()) {\n                    result = (result.compareTo(s1) < 0) ? result : s1;\n                }\n                \n                if (s.charAt(i) == \'1\') {\n                    c--;\n                }\n\n                i++;\n            }\n        }\n\n        return result;\n    }\n\n}\n```
['String', 'Sliding Window', 'Java']