Problem Statement | |||||||||||||
You wish to impress your friends by memorizing several thousand digits of pi. You believe it is easiest to memorize groups of 3 or 4 digits at a time, and decide on the following rules for assigning a complexity value to any group of digits: all digits equal ("333" or "0000", but not "2233"): 1 powers of 2, with no leading zeros ("512" or "4096", but not "0064"): 2 consecutive ascending digits ("012" or "5678", but not "1357"): 4 consecutive descending digits ("987" or "3210", but not "1098"): 5 first and last digits equal ("858" or "1761", but not "8882"): 7 any two digits equal ("655" or "0777", but not "9753"): 8 all other groups ("832" or "2049" ): 10 A group of digits should be assigned the lowest complexity value of the rules it matches. For example, the group "777" matches two rules (all digits equal and first and last digit equal), and it is assigned a complexity value of 1. You are to write a method that takes a string of digits and breaks them up into groups of 3 or 4 such that the sum of the complexities for all the groups is minimized. For example, the string of digits "2562222567" could be broken up into either "256 222 2567" with a complexity of 2+1+10=13, "256 2222 567" with a complexity of 2+1+4=7, or "2562 222 567" with a complexity of 7+1+4=12. The lowest of these three values is 7, so the correct answer is "256 2222 567". The input will be provided as a String[] digits. Each element of digits will contain only digits ('0' - '9', inclusive). Use the concatenation of all elements as the input to your method. You are to return the same digits as a String[], with a single space inserted between adjacent groups in the same element. Each element can contain a maximum of 100 characters. When populating the String[], put as many groups as possible in the current element before starting a new one. A single group may not span multiple elements, and elements must not contain leading or trailing spaces. There may be multiple ways to segment the string of digits that result in the same difficulty. Given two such segmentations, consider the first group at which they differ, and select the segmentation with the three-digit group over the one with the four-digit group. For example, the digits "2222225555555" could be segmented as "222 222 555 5555" or "222 222 5555 555", each with a difficulty of 4. The first difference is the third group, so we select the first choice because its third group has 3 digits. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | digits must contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of digits must contain between 1 and 50 characters, inclusive. | ||||||||||||
- | Every character in digits must be a digit ('0'-'9'). | ||||||||||||
- | There will be at least 6 characters total in digits. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|