返回介绍

lcci / 01.06.Compress String / README_EN

发布于 2024-06-17 01:04:43 字数 4449 浏览 0 评论 0 收藏 0

01.06. Compress String

中文文档

Description

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).

Example 1:


Input: "aabcccccaaa"

Output: "a2b1c5a3"

Example 2:


Input: "abbccd"

Output: "abbccd"

Explanation: 

The compressed string is "a1b2c2d1", which is longer than the original string.

Note:

  • 0 <= S.length <= 50000

Solutions

Solution 1: Two Pointers

We can use two pointers to find the start and end positions of each consecutive character, calculate the length of the consecutive characters, and then append the character and length to the string $t$.

Finally, we compare the lengths of $t$ and $S$. If the length of $t$ is less than $S$, we return $t$, otherwise we return $S$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

class Solution:
  def compressString(self, S: str) -> str:
    t = "".join(a + str(len(list(b))) for a, b in groupby(S))
    return min(S, t, key=len)
class Solution:
  def compressString(self, S: str) -> str:
    t = []
    i, n = 0, len(S)
    while i < n:
      j = i + 1
      while j < n and S[j] == S[i]:
        j += 1
      t.append(S[i] + str(j - i))
      i = j
    return min(S, "".join(t), key=len)
class Solution {
  public String compressString(String S) {
    int n = S.length();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < n;) {
      int j = i + 1;
      while (j < n && S.charAt(j) == S.charAt(i)) {
        ++j;
      }
      sb.append(S.charAt(i)).append(j - i);
      i = j;
    }
    String t = sb.toString();
    return t.length() < n ? t : S;
  }
}
class Solution {
public:
  string compressString(string S) {
    int n = S.size();
    string t;
    for (int i = 0; i < n;) {
      int j = i + 1;
      while (j < n && S[j] == S[i]) {
        ++j;
      }
      t += S[i];
      t += to_string(j - i);
      i = j;
    }
    return t.size() < n ? t : S;
  }
};
func compressString(S string) string {
  n := len(S)
  sb := strings.Builder{}
  for i := 0; i < n; {
    j := i + 1
    for j < n && S[j] == S[i] {
      j++
    }
    sb.WriteByte(S[i])
    sb.WriteString(strconv.Itoa(j - i))
    i = j
  }
  if t := sb.String(); len(t) < n {
    return t
  }
  return S
}
impl Solution {
  pub fn compress_string(s: String) -> String {
    let mut cs: Vec<char> = s.chars().collect();
    let mut t = Vec::new();
    let mut i = 0;
    let n = s.len();
    while i < n {
      let mut j = i + 1;
      while j < n && cs[j] == cs[i] {
        j += 1;
      }
      t.push(cs[i]);
      t.extend((j - i).to_string().chars());
      i = j;
    }

    let t = t.into_iter().collect::<String>();
    if s.len() <= t.len() {
      s
    } else {
      t
    }
  }
}
/**
 * @param {string} S
 * @return {string}
 */
var compressString = function (S) {
  const n = S.length;
  const t = [];
  for (let i = 0; i < n; ) {
    let j = i + 1;
    while (j < n && S.charAt(j) === S.charAt(i)) {
      ++j;
    }
    t.push(S.charAt(i), j - i);
    i = j;
  }
  return t.length < n ? t.join('') : S;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文