Given a string containing only dots and asterisk. A dot represents free spaces and represents lamps. A lamp at position can spread its light at locations i-1, i and i+1. Determine the minimum number of lamps needed to illuminate the whole string.
Input: str = “……”
There are initially no lamps so the whole string is in dark.We will install lamps at position 2 and 5.The lamp at position 2 will illuminate 1, 2, 3 and lamp at position 5 will illuminate 4, 5, 6 thus the whole string is illuminated.
Input: str = “*.*”
Approach: If we don’t have any asterisk then for each 3 dots we need one lamp, so the answer is ceil(D/3) where D is the number of dots. The problem can be solved by creating a copy of the given string, and for each asterisk in the first string we place an asterisk at it’s adjacent indices in the second string.
So if the given string is “…**..” then the second string will be “..****.”.
After that, we count the number of dots in each block of consecutive dots and find the number of needed lamps for that block, For each block the answer will be ceil(D/3) and the total sum of these lamps will be the answer for the complete string.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum number of letters needed to make a total of n
- Minimum number of Appends needed to make a string palindrome
- Minimum number of steps needed to remove the substring K from given string
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Least number of manipulations needed to ensure two strings have identical characters
- Number of closing brackets needed to complete a regular bracket sequence
- Minimum Word Break
- Minimum number of bombs
- Buy minimum items without change and given coins
- Minimum cost to modify a string
- Sum of minimum and the maximum difference between two given Strings
- Minimum rotations required to get the same String | Set-2
- Minimum cost to make two strings same
- Minimum Distance Between Words of a String
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum number of deletions so that no two consecutive are same
- Form minimum number from given sequence
- Lexicographically minimum string rotation | Set 1
- Minimum clicks to convert string X to Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.