We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母(a至z)。
Input: "aabcccccaaa" Output: "a2b1c5a3"
Input: "abbccd" Output: "abbccd" Explanation: The compressed string is "a1b2c2d1", which is longer than the original string.
0 <= S.length <= 50000
The text was updated successfully, but these errors were encountered:
/** * @param {string} S * @return {string} */ var compressString = function(S) { if (S.length <= 2) { return S; } let base = 0; let result = ''; for (let i = 1; i <= S.length; i++) { if (i === S.length || S[base] !== S[i]) { result += S[base] + (i - base); base = i; } } return S.length > result.length ? result : S; };
Sorry, something went wrong.
No branches or pull requests
《程序员面试金典(第 6 版)》 01.06. 字符串压缩
字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母(a至z)。
Example 1
Example 2
Note
0 <= S.length <= 50000
The text was updated successfully, but these errors were encountered: