Problem Statement
Limak had an empty grid, divided into square cells. He chose some non-empty rectangle (with sides parallel to sides of the grid) and marked all cells inside the rectangle.
A grid with H rows and W columns can be encoded into a string of length H*W as follows: examine the cells of the grid in row major order, and write down '#' for each marked cell and '.' for each empty cell. In other words, write down the first (topmost) row from the left to the right, then the second row from the left to the right, and so on.
Limak wanted to write down a string representing his grid. Unfortunately, he is easily distracted and thus he missed some cells (maybe zero or all of them). In other words, he got a subsequence of the string representing his grid.
You are given a
You may choose any positive dimensions for the grid. All missed cells count: both empty and marked ones.
Definition
- Class:
- Subrectangle
- Method:
- minMissed
- Parameters:
- String
- Returns:
- int
- Method signature:
- int minMissed(String S)
- (be sure your method is public)
Notes
- It can be proved that for any valid input string S there is some valid grid such that S is a subsequence of the string that encodes the grid.
Constraints
- S will contain between 0 and 300 characters, inclusive.
- Each character in S will be either '.' or '#'.
Examples
"..###.##"
Returns: 2
It's possible that Limak's grid looked like this: ..### ..### This grid is represented by the string "..###..###". Limak missed 2 cells in this case: ..###.(.)##(#). Here, we denote missed cells by putting them in brackets. It's the minimum possible number of missed cells, so the answer is 2.
"#.##.#.#.."
Returns: 2
The following grid is represented by the string #.#(.)#.#.#.(#). #. #. #. #. #. #. The second-to-last character could also be a dot (meaning that the last row in the grid is empty).
"####"
Returns: 0
It's possible that Limak had a grid of size 1x4 or 2x2 or 4x1 and he marked the whole grid. The grid would be then represented by the given string "####".
""
Returns: 1
"........................................"
Returns: 1
"###...###"
Returns: 3
".#.###.##...#.####.###...###.##"
Returns: 15
".................................................##..##...##.............................."
Returns: 5
"."
Returns: 1
"#"
Returns: 0
".."
Returns: 1
"#."
Returns: 0
".#"
Returns: 0
"##"
Returns: 0
"..."
Returns: 1
"#.."
Returns: 0
".#."
Returns: 0
"##."
Returns: 0
"..#"
Returns: 0
"#.#"
Returns: 1
".##"
Returns: 0
"###"
Returns: 0
"###."
Returns: 0
".#..#"
Returns: 1
"..#.##"
Returns: 2
"......#"
Returns: 0
"....####"
Returns: 0
"##..#..##"
Returns: 3
".###.#..#."
Returns: 4
".##...#..##"
Returns: 4
"..##...#...."
Returns: 3
".##..#.....##"
Returns: 7
".##.#.#..#...."
Returns: 2
"#.#.##..#.#.#.#"
Returns: 3
"######.##.##########.##"
Returns: 7
".####..###.#####.###..##"
Returns: 11
".#...#............#.....#"
Returns: 7
"####################################"
Returns: 0
".................##.....#............"
Returns: 3
"#####.##########.########.###.####.########.###"
Returns: 13
".#...#....#..#.#.##...#...###.......##..##..#..#.##...##....."
Returns: 29
"#.......#.#..##...#..#..##.##.#.....###.##.#......#.#.#.#.##....#."
Returns: 30
".#....#######.######...#.###...#####.#.#.#.#...#......#.....###.#.##..#"
Returns: 39
"##.##.######..################..######.#####....####.###.######.##.#.##.##..##.##.##..#.#.##"
Returns: 37
"###########.#..##.###.###############.#.####.#######...#######...#######.######..####..##...#####.."
Returns: 57
"...........#.......##...#.##.....#.#..........#.#.....#..#.................##...#...##.....##.#.........."
Returns: 47
"...##...#..#...#..####..#......##......#...#.....#..#.#..#.###.##..####......##.###..##..##..###....##...##"
Returns: 58
".#.#.#....##..#......##.....#.#.#.#.#..#..###........##.......##........#...##.###......#........#.#.......#"
Returns: 54
"#.#............##....#.......#.##.....#....##....#.#.......#.......#........#..#...##.#........##...#..#..#.##...##"
Returns: 56
"#.######..#########..#..##.##.#.#########....###.###.########.##.....####.#.##.############.####.####.######.##########"
Returns: 58
"...##.####..#.###.##...####.#...#.....###....#....#####...#..##.######.....#.#..##....##..##.#.####.#.##.##.###....##...##."
Returns: 67
"..........##...#...................#.#.......#...##....#...#..#.......##..#....#.#...#...............#...#..#.#......#.#..##."
Returns: 51
".#.##..###.##..#.###...###.#.####.#.######....##..#####.#.#.#..##.##.#.#.##.###.#.#....#.################.##.#.#.##.###.###..##..#..###.#"
Returns: 65
"..............#..#.....#.#....#.....#...##....###...#...#.........#.....#.....#.....#..........#........#.#..#.............#.......#.#....#..#"
Returns: 56
"#####..#..##..####.#.#######.#..#####.###...#.####.###.###....#.##.#..#...#########...##.######.#...##..#....######.##....#.#..###.##.##..##...#####"
Returns: 86
"....#..#..##.......##........#.#....#...#.###.#....#...##.......#.#...#.#.#.##....###..#...#..##.#...#............##....#.....####.....#.#...#......#......#..#..#."
Returns: 77
"####..#..########.#########..########..##.#########.#.#########..#.##.....#..#.#..###.##.###.#.#.######..###.##########..#.#.######.#######.#.#.#####.##############..##."
Returns: 89
"##.###..##..#####.#.#..#.#.##.#####.##.#####....##.##...##................##.#....##.....#.##....#.####..#....###.#.#.##.......#...####.##..#..##...#...#...#.....####.#..#.."
Returns: 97
"###.##.######...#..###.##.###..#...###..#######.####..#####.##.##.###.#.##....#######..#..###.#.#####.##..#.##...#..###.##......##.#######.###########..##########.####.#.####"
Returns: 102
".####..##.....#.#..##..##.#..........#..#.....##...#.###.......##.#...##.#....#..#.#..#.#.#.#.#...#..###....##..##...##.....##.##..##.#...#...#.....####.#...##...##..#####.##....###"
Returns: 97
".#........#...#.............#........#..........................#..........#.....#.........##...#..............#.................#......#..#.##................#......#..........#....."
Returns: 65
"..#....#..####..#..#.#.#.###...#..#..#.#......#....#.....##.#.##.#....#.#.......#....#...#....##.###...##...#.#.#.###.....###..##....#....####.###...#......#..#..#.##..#.....##.##.####"
Returns: 96
"##########################################################################################################################################################################################"
Returns: 0
"#######..######.#######.##..#.###.#....##.#######.#..############.########.##.##.##.######.######.#################..##.#.#..#####.####..####.#.####..###.###.#.###.#..#..###.###.#.#######"
Returns: 92
"..#.#.#..#.#..##.##.#######...##.#######..################..########..########....#.#.#.##..##.####......#..##.#.#.##.####.##.###....#.#.##.###..###..###.###..####.######.##....#.###.#.###"
Returns: 106
"###...##.####.##.#.##.....#.###.####..##..##.#####....#...#..#.#..#.#......#..#.##.#..#.##.##.#..###.#.###..#.##.##...#..#.##.##.######.#.####.#.#..###.....#.#.#......##.#.##.#..#.##...#..#.#."
Returns: 84
"..............................#..........................................#..........................#............................................................................................."
Returns: 14
".....#........#................#..#.#.......#.........#.....#.#...#......#..##...#.....#.............###.#.............#...#...................#.............#..........##....#..........#.#...#...#.."
Returns: 82
".####.##..##.#.##.#..##..#...#..#####.#...#..###...#.#..#.#..##..###.#...##.##..#.......###..#...#.##.#..#.##.##....##..##..#.##..#.##..#..##.###....##.....#.#.#.#########.#...#######..#..#.....#..####"
Returns: 99
"###########...######.########.##.###########.###..#...##.##.#.#.#.#######.###.##.##.#.#.##########.###.###.##.##.###.##.###..#####..######.###..###########################.##.###..#.##.#.####.##.###.####.##"
Returns: 91
"###.#.######.#############.###.#######.#########.###.######.##.########.####.###.#####.#.####.###.#############.#####..##.#################.###.#########..############.#######.#########..##########.#########"
Returns: 73
"..#...#......#.......#.#......#........#.#..........#..#......#.#............#.....##..#.#.###..#....##..#.......##...#......#...##....###.#..##.....####...#..............#...##.##..###....#......#.##.#...#.."
Returns: 101
"######.######.#######################.##.##..#.#######.###.#########.########.###.################.#.############.##.####################.######.###.#######.######.############..################.##.######.##.###"
Returns: 69
"#..#....#..##.#..#.......##....##.##.#...##.........#...######.......##.##.##.##..###.##.....##.........##..#.##.#..#...##..#...##..######......#.#.###..#####.###..##.........#.##........###.#.###.#..####.#..##..."
Returns: 123
".....#.......#............................................................#............#............#...........................................................................................................#............"
Returns: 26
"#.####.##.##.########.################################.#..######.####.####.############################.#############.#######################.####.###.##.###.#####.###.##...##############.#####..###.####.###########.#####.#.#"
Returns: 90
"###.##..#..#.#############.#..#.######.##..#####.##.#.#.###.#######..######.######.#########.##.###..#.###.########.####..#.###########..#.##.############...#####.#.#.###..##..#.#####..#.########.#####..##.#.####.#.##..#######."
Returns: 112
"....###.##.##.#.#####.#.#.##.###.#.###.###.#..##.##.#..#####.#...##..#.#..#.#####...#..###..#.###.##..###..###..##.#.####......#..#.###.#####.#...#..####...###.##########.#.##.#..#..#.#..##..##.##..####.#....##.##..##.#..#.#.####"
Returns: 107
"..........................................#..........................##..........................#.............................................................................#.......#..............................................."
Returns: 29
"#####.#####.#####.###.###.######.#..#.##############.############################.#####.#####.#.#.############.######..#####.######.##.######.#################################.#######.##################.###################.#############"
Returns: 84
"####.####.###.#.#.###.######.###...#.#####.#.#.###..###.#.#.##.###.##.####.###.####.#.....#######.###.##...#..#####.#.####..#..##...##.#.#####.#####..###.######.#.#.####.##.###.#####.######.##..#..#..#...###.####.##.####.####..#.#.#.######..##"
Returns: 123
"###.#########.#####.#.##.#.#######.#.#.#.##.#####.#####..##...####.###.######.#######.#######.#.####.###.#.###.#..#.#.#####.###....##.#..##..#.##.######.####.####.###.#.#.##..#..####.##...##..#..##.##.#########.....#.#######.###..#.##############"
Returns: 126
".......#....#..#.##.#....#..........#..#.#........#.......#....#.......#......#.#......................#....#.#..#..#....##.............#......................#.............#.#.##.....#.......##................#......#......#...#..#...................."
Returns: 93
".........................................................#.....#.............#.#......................................................#....#.......................................#...............................................#......##................."
Returns: 48
"#########################################################################################.###############################.#####################..####################.################################################################################.#######"
Returns: 50
"######..###.################################.###########..######.#####.####.#########..#.###########.############.###########.##.##.#######################.######.#######################.#######..######.#####.######.##.##.#.###.####.###########..#############"
Returns: 97
"########.##########.#######.##########.##.######.#######################################.##.#######.##.####.#####.##################.#####################.####.#..##################.######.####.##..#####.################.#####.################.####.############"
Returns: 89
"####.#.##.##....###....#...#.#.##...#..##.##..###...##.######.####.#.#######..##.###.##.##..####....####.#.#..#..#####.#########.##.#.#..###########.#.####.####..#######.########.##..#...#..#########.##.#.####################.###.#.#.#..#####.....#####..########.#"
Returns: 150
"######.######.###########.##########.##########.#.####.############..##.##########.##########..##################..#.#################.###########.####.#########..######.#############.####################.#####.##########.######.##########.##.##.###########.####.###"
Returns: 105
".........#.#.#...........#.......#....#.......#.........#...#...#......#..#.#...#......#.........##....#.#.......#..........###.....####.#..#.....#.......#.....###.................###..#.#.........#.........#..#...#......##...##....#........#.#...#....#.##..........#"
Returns: 129
"#.##..#.##.##.....#.#.#.#...#.####.#.######.#.##.##.###.......#..#.####...##....#..#.##..###.#.####.##.#.#...#####.##.#....#.#......##..#..##..##......#####..#.###.##..##..##.#.....#.#.##.#.#...##..####.#....##..#....#....######..#####..#..##.##.####..##.#..##.#..#.#.##.#"
Returns: 132
"...#.#....##.##......#......#.#.#..#.####.#..#..#...##..##......###...#....##.#..#.#.##..#####.##..###...#...#....#...#.#.###.#.......##.#......#....#..##..#..#...#.#.#...#.##..##...##..#.....##...#.#.....##.#.#....##...#...##.....#...........#.##.#....#...##....##...##.#...#......#"
Returns: 147
".#.####.##.#.#.#####....###.#.########.#.##..##...##.##.#####.##.##.##.#..##..#..#..###########....##.#.#####..#.###..###.#...##.###.##.#####.#####.####..#.#...#######.###.###.#..##.##.##..###..##..##.##.#..####.##.####.#...#...#######.##.##...##..##...##.#####..#.###.##########.#.#.#.#"
Returns: 149
".##.#.....######.##.####..#.###.....##.###.####.##.#..#.##########.##.#.####.#.####.##...###.###.#...#...#.#.##..#.###...##.####...##.#.##.##...#..##.##.#####.##...##..#.#.##.#..##########...#.##..#.####..#####.#######.##.###..###.#.###......#####.#.####.####.#####.#....#####.####...####"
Returns: 156
"#.####..##.#...##..##.#......#....#.##..##.#....##.......#.....###....#..#.#.....#.#.##...##.####.....##.#.#..#.##..#.#....#......#.###..#..#.##.###.....#...##.#...#..#.###.....#...##.........#...#.#.#.###.#.#.#.....#.##....#..#..#.#.##..#..#....#...#.....###.....##.##.#.#.###...##.......#...#"
Returns: 150
".#.##...#..###.#.##...#...#..#.####..#.#..####.#.##.#....##..##...#..#.#######.....##.#.#....#....#.##..#..#.#.#..#.##.#.###.#..#...#.#...#....##.####.####..##.#.#...##.##.#...####.##.#...##.###.###...#.#.##..#....#######.....#.##.#..###.#####.####.#..#..##...##.##.#..#...#..####.##..#.##.###.#.#."
Returns: 136
"#..#####..#.#.###.....#......#######.###.#....#.####...#....###..#.##.....####..#.#####.#...##....#.###.#.###.##.##.#.#.##..##..###...##...#.#####..##...##.#...#.#.#.#....###...#.##..#.##.....#.##...#......#..##..#....#.###.##...##..##.##.##.###...##.###.#..##....#####....#.####.###..###.##..#.#..#"
Returns: 155
"########.#..#...###.##.#.#.##..#...#.#..#.##.#.#..##..#..#.....##...#......#..#.##.##..###.##..#..###.#...#.....##.###.#..#.#..###..#...#.#......#.#.#.##....#..##....###....##.###..#...##....#....#..##..#.####.....#.##.#..##.#...###.##.###.######.#####.....#.#.#....#####.##.#....#...#.#.####.###.#.#"
Returns: 146
"..###..###......#...###....#..#.###....#..#...##.#.#...#...######.#..##.##.#.####.#.###.#..##..##.###..###.##.##.#..##.#.#..#...##.#.#.#...##...#.######.#.##...##..##.##.##.#.###.###...#.##.....#.#.#..##...#..#..#.#...###.#..#.#####..##..#...#...##....#..####...###.#.....#..##.###.#..##..#####.###.#"
Returns: 142
"...#...##.##.#.#.##.##.#.##.##....####.###.###..##.##.##.###.....###.#...#..#....#.#.###.###.###....#.###.##.##......#.#.#.....#####.###.#######.##..#..###.#######..#.##..##.#.#.######......#.##....##.#.#####....##.##.###..###...#...#.#.###..##.#..##.#.#.#..#.#....#.#.##.#.##.#.##.###.#.##..#.###..."
Returns: 138
"..#..#..#.###.##..#.....#.##.##..#.#....#...#.#....####.#.....###.....##.##.#...#..##...#..............#.....#...#..####....##...#........#.#............#..#..#.####.#..#..#.##..#..#....#..##...#.#....###..##...#...##.#..#....#..#.#.....##.####..#.#...#...#....#..#..##...#.#........###..##.#.#.##..#"
Returns: 153
"#...##.##########.#######.######.#.##...#..############.###.##.#.#.###.##..#####..#.#...########.######.#.##.#.#.###..#..##.##..###.##.####.##.#.#.####..#.#.###.#.####.##.#..#.####.#..####.#..#.####..#.##.###..#.####.#.####..#####.##..###.####...#########.###.#.#.#########..####.##.##.########.####."
Returns: 138
"....#........##....#.......#................................#.........................#........#...#......#................#.......#.......#.........#..........................................#.................#........#.....#...#...#.......##..........#..................#..#.#.......#..#..........."
Returns: 95
"############.###.######################.######.###.##############.################.###############.##########.###################...#######.##############.############.#.##.##########.#########.######.#######.###.#######.#.################.#.######.#####.#.###.##.#######.##.######..#################"
Returns: 112
".......................#.............#.....................................#.#.........................................................#...........................#.................................................................................................................................#......"
Returns: 52
"######################################################.##################################################################################################################################################################################.#.##########.###.#################################################"
Returns: 52
"#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#."
Returns: 0
".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#"
Returns: 0
"#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#"
Returns: 1
"###.###..####.#.#.#.#.#..##.###...###.#...#.#.#.#.#.#.#.#.#.#.....#.#####.#.#.#..##.#.#.#...#.#.......#.#.#...#...#..##.###...#.#.#.#.#.#.#.#.#.#.#...#.#.#.#.#...#.#.###.#...####.####.....#.#..##.#.##..#.#.#...#.#...#.#.#.###.....#.#.#.#.#.#.#.......#.#.#.#.#.###.#.#.#.###.###.#.#...#.#.###.#.#....#"
Returns: 98
".#.#.#.##........###...##..#.#.#.###.#...#.#.#####.##..#.#...#..#..###.#.#.#..#.#.##.#.#...#####.#..#.....##...#.#.#.#.#.#....####.#.....#.###.#.#.#.#.###.#.#.....#.###.#.#.#.#.#.#.#.#.###.#.#.#...#.#.#.#.....#.#.#...#.#.#.#...###.#.##..#.#.#####.#.#.#.###.#.#.#.#.#.#.##.##.#.#...#.####..###.###.#."
Returns: 101
"....................................................................................................######################################################################################################################################################.................................................."
Returns: 0
"............................................................................................................................................................................................................................................................................................................"
Returns: 1
"############################################################################################################################################################################################################################################################################################################"
Returns: 0
".......######################################################################################################################################################.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..."
Returns: 134
"..#############################################################################################.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.................................................................................................#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..."
Returns: 188
"######################################################################################################################################################..##..##..##..##..##..##..##..##..##..##...................................................................................................."
Returns: 88
"...######..................................................................................................................................................................................................................................................................................................."
Returns: 0
".......................................................................................................................................#####################......#####################......#####################......#####################......#####################......#####################......"
Returns: 0
"....................................................................................................................................................................................................................................................................###########################............."
Returns: 0
"#####.....####.#..#..#..#..#....#...#.####.##...##...##..##...####...##.#.....##...##.##.###..###....##..#.##.#.###.#..#....#.##.#.##.#..#..#.#..#.#...#.#.#.#...#...##..#.#...#.##...##.###.#....#.###.####.##.........#.####.##..##...#..#.#########...##..###.#..#.###.##.#....#...#....##.##.#.###..#..."
Returns: 144
"#..#.#..######..###...###.##.##.#.###.#.########.##..####.##..######..#####..#..#####.##.###.##.###...#.#..###.###.#.###.########.##..#.#.....##.##.###.#######.###.##..#..##.#...###..###.#####.########.#######.#.##.###.######.#.#.###.#.###..#####.####.###.#####..##..#.###.####.#####.######.#.#######"
Returns: 156
"#.#.....#...........#.#.#.#.#.#.#...#...#.....#.....#.....#.#...#...#...#.#.#...#.#...#.#...#.#.#...#.....#.#.#.#.#.#.....#...................#...#.......#.#...#.#.............#.....#...#.#.......#.#.#.#.#.......#...#...#.#.......#.#.#.#.......#...#.#.#...#.......#.......#.#.#.#.#.#.#.#.#.#.#...#..."
Returns: 144
"##.##.##...."
Returns: 0