Shortest and Lexicographically Smallest Beautiful String - Complete Solution Guide
Shortest and Lexicographically Smallest Beautiful String is LeetCode problem 2904, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a binary string s and a positive integer k . A substring of s is beautiful if the number of 1 's in it is exactly k . Let len be the length of the shortest beautiful substring. Return the lexicographically smallest beautiful substring of string s with length equal to len . If s doesn't contain a beautiful substring, return an empty string . A string a is lexicographically larger than a string b (of the same length) if in the first position where a and b differ, a has a character st
Detailed Explanation
The problem asks us to find the shortest and lexicographically smallest substring of a given binary string 's' that contains exactly 'k' ones. If no such substring exists, we should return an empty string. The problem emphasizes two criteria: length and lexicographical order. We want the shortest substring that has k '1's, and if multiple substrings have that shortest length, we pick the one that comes earliest in lexicographical order (like in a dictionary).
Solution Approach
The solution focuses on efficiency by first identifying the indices of all '1's in the input string 's'. Then, it iterates through these indices, creating substrings that contain exactly 'k' ones. For each substring, it checks if its length is shorter than the current minimum length. If it is, it updates the minimum length and the result substring. If the length is equal to the current minimum length, it compares the current substring lexicographically with the current result and updates the result if the current substring is smaller.
Step-by-Step Algorithm
- Step 1: Create a list/vector/array called `one_indices` to store the indices of all characters '1' in the input string `s`.
- Step 2: If the number of '1's in `s` is less than `k`, return an empty string because a beautiful substring cannot exist.
- Step 3: Initialize `min_len` to infinity (or a very large number) and `result` to an empty string. These will store the length of the shortest beautiful substring found so far and the substring itself.
- Step 4: Iterate through the `one_indices` list from `i = 0` to `len(one_indices) - k`. This loop considers each possible starting index for a beautiful substring.
- Step 5: Inside the loop, get the starting index (`start_idx`) from `one_indices[i]` and the ending index (`end_idx`) from `one_indices[i + k - 1]`. These two indices define a substring that contains exactly `k` ones.
- Step 6: Calculate the length of the current substring: `current_len = end_idx - start_idx + 1`.
- Step 7: Compare `current_len` with `min_len`. If `current_len < min_len`, update `min_len = current_len` and set `result = s[start_idx : end_idx + 1]` (or the equivalent substring operation in the respective language).
- Step 8: If `current_len == min_len`, compare the current substring `s[start_idx : end_idx + 1]` with `result` lexicographically. If the current substring is lexicographically smaller than `result`, update `result`.
- Step 9: After the loop finishes, return the `result` string.
Key Insights
- Insight 1: Pre-calculating the indices of '1's in the string significantly improves efficiency. Iterating through all possible substrings to count '1's would be much slower.
- Insight 2: Finding the shortest beautiful substring first allows us to prune the search space. We only need to compare substrings of the minimum length found so far.
- Insight 3: Lexicographical comparison using the language's built-in string comparison operators is the most efficient way to compare substrings of the same length.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Sliding Window.
Companies
Asked at: IBM, Wells Fargo, Yelp.