[Leetcode] Encode string with shortest length encoded strings

Source: Internet
Author: User
Tags encode string

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:

    1. K would be a positive integer and encoded string would not be empty or extra space.
    2. You may assume this input string contains only lowercase 中文版 letters. The string ' s length is at most 160.
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.