Given a non-empty string, encode the string such that it encoded length is the shortest.
The encoding rule is: k[encoded_string]
, where the encoded_string inside the square brackets is being repeated exactly k< /c3> times.
Note:
- K would be a positive integer and encoded string would not be empty or extra space.
- You may assume this input string contains only lowercase 中文版 letters. The string ' s length is at most 160.
- If an encoding process does does the string shorter, then does not encode it. If There is several solutions, return any of the them is fine.
Example 1:
Input: "AAA" Output: "AAA" Explanation:there is no-i-encode it such that it's shorter than the input string, so we do Not encode it.
Example 2:
Input: "AAAAA" Output: "5[a]" explanation: "5[a]" is shorter than "AAAAA" by 1 character.
Example 3:
Input: "Aaaaaaaaaa" Output: "10[a]" explanation: "A9[a]" or "9[a]a" is also valid solutions, both of them has the same Len Gth = 5, which is the same as "10[a]".
Example 4:
Input: "AABCAABCD" Output: "2[aabc]d" Explanation: "AABC" occurs twice, so one answer can "2[aabc]d".
Example 5:
Input: "ABBBABBBCABBBABBBC" Output: "2[2[abbb]c]" Explanation: "ABBBABBBC" occurs twice, but "ABBBABBBC" can also is encode D to ' 2[abbb]c ', so one answer can is "2[2[abbb]c]".
S
[Leetcode] Encode string with shortest length encoded strings