📌  相关文章
📜  打印给定字符串中相邻重复字符的频率

📅  最后修改于: 2022-05-13 01:56:05.652000             🧑  作者: Mango

打印给定字符串中相邻重复字符的频率

给定一个长度为N的字符串str 。任务是打印相邻重复字符的频率。

例子:

方法:这个问题可以简单地通过遍历和跟踪相邻的重复字符来解决。请按照以下步骤解决给定的问题。

  • i = 0迭代直到字符串长度。
  • 维护一个柜台。
  • 再次通过从i+1到字符串长度的下一个循环进行迭代。
  • 计数器将递增,直到下一个字符不同。
  • 对于频率超过2 个的字符,增加i以使计数保持不变。
  • 如果计数器大于1 ,则打印。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find frequency
// of repeating characters
void concesStr(string str)
{
 
    // Length of string
    int lenStr = str.length();
 
    // Iterate through 1st pointer
    for (int i = 0; i < lenStr; i++) {
 
        // Keep a counter
        int curr_count = 1;
 
        // Iterate through 2nd pointer
        for (int j = i + 1; j < lenStr;
             j++) {
 
            // if next element is different
            // then break
            if (str[i] != str[j]) {
                break;
            }
            curr_count++;
 
            // Example: if count is 3
            // then move the first
            // pointer so that
            // count remains intact
            if (curr_count > 2) {
                i++;
            }
        }
 
        // Condition for print more than 1
        // count characters
        if (curr_count > 1) {
            cout << str[i] << ": "
                 << curr_count << endl;
        }
    }
}
 
// Driver Code
int main()
{
    string str = "Hellolllee";
 
    concesStr(str);
    return 0;
}


Java
// Java code to implement above approach
import java.io.*;
 
class GFG {
 
    // Function to find frequency
    // of repeating characters
    public static void consecStr(String str)
    {
        int lenStr = str.length();
 
        // Iterate through 1st pointer
        for (int i = 0; i < lenStr; i++) {
 
            // keep a counter
            int curr_count = 1;
 
            // Iterate through 2nd pointer
            for (int j = i + 1; j < lenStr;
                 j++) {
 
                // if next element is different
                // then break
                if (str.charAt(i) != str.charAt(j)) {
                    break;
                }
                curr_count++;
 
                // Example: if count is 3 then
                // move the first pointer
                // so that count remains intact
                if (curr_count > 2) {
                    i++;
                }
            }
 
            // Condition for print
            // more than 1 count characters
            if (curr_count > 1) {
                System.out.print(str.charAt(i)
                                 + ": " + curr_count
                                 + "\n");
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        consecStr("Hellolllee");
    }
}


Python3
# Python code to implement above approach
 
# Function to find frequency
# of repeating characters
def consecStr(str):
    lenStr = len(str);
    i = 0;
     
    # Iterate through 1st pointer
    for k in range(lenStr):
 
        # keep a counter
        curr_count = 1;
 
        # Iterate through 2nd pointer
        for j in range(i+1,lenStr):
 
            # if next element is different
            # then break
            if (str[i] != str[j]):
                break;
 
            curr_count += 1;
 
            # Example: if count is 3 then
            # move the first pointer
            # so that count remains intact
            if (curr_count > 2):
                i += 1;
 
        # Condition for print
        # more than 1 count characters
        if (curr_count > 1):
            print(str[i] , ": " , curr_count , "");
        i += 1;
 
# Driver code
if __name__ == '__main__':
    consecStr("Hellolllee");
 
# This code is contributed by 29AjayKumar


C#
// C# program for above approach
using System;
 
class GFG
{
  // Function to find frequency
  // of repeating characters
  static void concesStr(string str)
  {
 
    // Length of string
    int lenStr = str.Length;
 
    // Iterate through 1st pointer
    for (int i = 0; i < lenStr; i++)
    {
 
      // Keep a counter
      int curr_count = 1;
 
      // Iterate through 2nd pointer
      for (int j = i + 1; j < lenStr;
           j++)
      {
 
        // if next element is different
        // then break
        if (str[i] != str[j])
        {
          break;
        }
        curr_count++;
 
        // Example: if count is 3
        // then move the first
        // pointer so that
        // count remains intact
        if (curr_count > 2)
        {
          i++;
        }
      }
 
      // Condition for print more than 1
      // count characters
      if (curr_count > 1)
      {
        Console.WriteLine(str[i] + ": " + curr_count);
      }
    }
  }
 
  // Driver Code
  public static void Main()
  {
    string str = "Hellolllee";
 
    concesStr(str);
  }
}
 
// This code is contributed by gfgking.


Javascript



输出
l: 2
l: 3
e: 2

时间复杂度: O(N 2 )
辅助空间: O(1)