gpt4 book ai didi

c# - 用字符串中的其他内容替换重复的字符

转载 作者:太空狗 更新时间:2023-10-30 00:31:51 25 4
gpt4 key购买 nike

我需要帮助来找出一个逻辑:

所以,假设我有一个字符串,每当该 string 中有一个 Char 重复时,我需要将其替换为 (Char + 数字序列)。

例如:

原始字符串:"abcdefgabfabc"

预期输出:"abcdefga2b2f2a3b3c2"

'a' occurs 3 times, so the first 'a' remains as 'a', but the second 'a' becomes 'a2', and the third 'a' becomes 'a3' and the same goes to other chars like b, b2, b3...

最佳答案

  1. 创建一个包含字母和每个字母出现次数的字典
  2. 创建一个 StringBuilder 来存储输出
  3. 逐个字母地遍历输入字符串
  4. 将字母输出到新字符串
  5. 如果一个字母不在字典中,则将其添加为键,以'1'为值
  6. 如果一个字母已经在字典中,则将该值加1并将该值追加到输出字符串中

关于c# - 用字符串中的其他内容替换重复的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com