✅Simple Java Solutions || ✅Runtime 2ms
 \n   Explanation of the code \n\n1. Initialize a `StringBuilder` named `res` to store the lexicographically smallest beautiful substring.\n\n2. The method uses
<details> \n  <summary> Explanation of the code </summary>\n\n1. Initialize a `StringBuilder` named `res` to store the lexicographically smallest beautiful substring.\n\n2. The method uses a nested loop structure to iterate through the binary string `s`. The outer loop, controlled by the variable `i`, starts from the beginning of the string and goes up to the `k` characters from the end of the string. This is because a beautiful substring with exactly `k` ones cannot be longer than `k`.\n\n3. Inside the outer loop, there\'s a conditional check to skip the iteration if the current character at position `i` is \'0\'. If it\'s \'0\', it won\'t contribute to forming a beautiful substring, so the loop skips to the next character.\n\n4. When the current character at position `i` is \'1\', it starts a new inner loop controlled by the variable `j`. This inner loop scans through the string from the current position `i` to the end, appending characters to the `str` StringBuilder.\n\n5. Within the inner loop, there\'s a `count` variable to keep track of the number of \'1\'s encountered in the `str` substring.\n\n6. The code checks if `count` has reached the desired value of `k` (the number of ones required for a beautiful substring). If `count` equals `k`, it means a beautiful substring is found. At this point, the code proceeds to compare `str` with the current `res` to determine if it\'s the lexicographically smallest beautiful substring.\n\n7. The `isSmaller` method is called to perform the comparison. It takes two `StringBuilder` objects, `s1` and `s2`, and compares them lexicographically. If `s1` is smaller than `s2`, the method returns `true`. If `s2` is smaller, it returns `false`.\n\n8. If the newly found `str` is smaller than the current `res` (or if `res` is empty), it updates the `res` StringBuilder to contain the contents of `str`.\n\n9. The loop continues, and the process repeats for other potential beautiful substrings in the input binary string `s`.\n\n10. After processing the entire input string, the method returns the lexicographically smallest beautiful substring found in the variable `res`.\n\nIn summary, the code finds beautiful substrings by iteratively exploring substrings of the input string `s`, checking for the presence of exactly `k` ones (\'1\'s), and comparing them to find the lexicographically smallest one.\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n![image.png](https://assets.leetcode.com/users/images/c5c2488d-9ab1-4999-a6be-142ce35ba4d6_1697546653.0342789.png)\n\n</details>\n\n# Code\n```Java\nclass Solution {\n    public String shortestBeautifulSubstring(String s, int k) {\n        StringBuilder res = new StringBuilder();\n\n        for (int i = 0; i <= s.length() - k; i++) {\n            if (s.charAt(i) == \'0\') continue;\n            StringBuilder str = new StringBuilder();\n            int count = 0;\n            for (int j = i; j < s.length(); j++) {\n                char c = s.charAt(j);\n                if (c == \'1\') count++;\n                str.append(c);\n              if (count == k && (res.isEmpty() || isSmaller(str, res)) ) {\n                res = new StringBuilder(str);\n                break;\n              }\n            }\n        }\n        return res.toString();\n    }\n\n    public static boolean isSmaller(StringBuilder s1, StringBuilder s2) {\n        int length1 = s1.length();\n        int length2 = s2.length();\n        \n        if (length1 == length2) {\n            int i = 0;\n            while (i < length1-1 && s1.charAt(i) == s2.charAt(i)) {\n               i++;\n            }\n            return s1.charAt(i) < s2.charAt(i);\n        }\n        \n        return length1 < length2;\n    }\n}\n```\n\n## For additional problem-solving solutions, you can explore my repository on GitHub: [GitHub Repository](https://github.com/ahmedna126/java_leetcode_challenges)\n\n\n![abcd1.jpeg](https://assets.leetcode.com/users/images/212f1539-9f19-4d65-abfb-8c38a1021bbf_1697548117.8503027.jpeg)\n
['Java']