📌  相关文章
📜  打印给定字符串的所有位置,两边的较小字符相等

📅  最后修改于: 2021-09-07 02:53:28             🧑  作者: Mango

给定一个字符串str ,任务是找到给定字符串的索引,使得该索引左侧和右侧按字典顺序排列的较小字符的计数相等且非零。

例子:

原始的方法:要解决这个问题最简单的办法是遍历定的字符串,依靠左侧和右侧给定的字符串的各指标的字典序小字符数。对于每个索引,检查当前索引左侧和右侧按字典顺序排列的较小字符的数量是否相等。如果发现为真,则打印当前索引。

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

高效的方法:优化上述方法的想法是使用哈希。请按照以下步骤解决问题:

  • 初始化一个数组,比如cntFre[]来存储给定字符串的每个字符的频率
  • 遍历给定的字符串和存储给定字符串的每个字符的频率。
  • 初始化一个数组,比如cntLeftFreq[]来存储出现在给定字符串的当前索引左侧的所有字符的频率。
  • 遍历给定的字符串并存储出现在当前索引左侧的所有按字典顺序排列的较小字符的频率。
  • 对于每个索引,检查当前索引左侧和右侧按字典顺序排列的较小字符的数量是否相等。如果发现为真,则打印给定字符串的当前索引。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find indexes
// of the given string that
// satisfy the condition
void printIndexes(string str)
{
    // Stores length of
    // given string
    int N = str.length();
     
    // Stores frequncy of
    // each character of str
    int cntFreq[256] = {0};
     
    for (int i = 0; i < N;
                      i++) {
         
        // Update frequency of
        // current character
        cntFreq[str[i]]++;
         
    }
     
    // cntLeftFreq[i] Stores frequncy
    // of characters present on
    // the left side of index i.
    int cntLeftFreq[256] = {0};
     
    // Traverse the given string
    for (int i = 0; i < N; i++) {
         
        // Stores count of smaller
        // characters on left side of i.
        int cntLeft = 0;
         
         
        // Stores count of smaller
        // characters on Right side of i.
        int cntRight = 0;
         
        // Traverse smaller characters
        // on left side of index i.
        for (int j = str[i] - 1;
                    j >= 0; j--) {
             
             
            // Update cntLeft  
            cntLeft += cntLeftFreq[j];
             
            // Update cntRight
            cntRight += cntFreq[j]
                   - cntLeftFreq[j];
        }
         
        // Update cntLeftFreq[str[i]]
        cntLeftFreq[str[i]]++;
         
        // If count of smaller elements
        // on both sides equal
        if (cntLeft == cntRight &&
                 cntLeft != 0) {
             
            // Print current index;
            cout<


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find indexes
// of the given String that
// satisfy the condition
static void printIndexes(char[] str)
{
  // Stores length of
  // given String
  int N = str.length;
 
  // Stores frequncy of
  // each character of str
  int []cntFreq = new int[256];
 
  for (int i = 0; i < N; i++)
  {
    // Update frequency of
    // current character
    cntFreq[str[i]]++;
  }
 
  // cntLeftFreq[i] Stores frequncy
  // of characters present on
  // the left side of index i.
  int []cntLeftFreq = new int[256];
 
  // Traverse the given String
  for (int i = 0; i < N; i++)
  {
    // Stores count of smaller
    // characters on left side of i.
    int cntLeft = 0;
 
    // Stores count of smaller
    // characters on Right side of i.
    int cntRight = 0;
 
    // Traverse smaller characters
    // on left side of index i.
    for (int j = str[i] - 1;
             j >= 0; j--)
    {
      // Update cntLeft  
      cntLeft += cntLeftFreq[j];
 
      // Update cntRight
      cntRight += cntFreq[j] -
                  cntLeftFreq[j];
    }
 
    // Update cntLeftFreq[str[i]]
    cntLeftFreq[str[i]]++;
 
    // If count of smaller elements
    // on both sides equal
    if (cntLeft == cntRight &&
        cntLeft != 0)
    {
      // Print current index;
      System.out.print(i + " ");
    }
  }
}
 
// Driver Code
public static void main(String[] args)
{
  String str = "aabacdabbb";
  printIndexes(str.toCharArray());
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement
# the above approach
 
# Function to find indexes
# of the given that
# satisfy the condition
def printIndexes(strr):
     
    # Stores length of
    # given string
    N = len(strr)
 
    # Stores frequncy of
    # each character of strr
    cntFreq = [0] * 256
 
    for i in range(N):
         
        # Update frequency of
        # current character
        cntFreq[ord(strr[i])] += 1
 
    # cntLeftFreq[i] Stores frequncy
    # of characters present on
    # the left side of index i.
    cntLeftFreq = [0] * 256
     
    # Traverse the given strring
    for i in range(N):
 
        # Stores count of smaller
        # characters on left side of i.
        cntLeft = 0
 
        # Stores count of smaller
        # characters on Right side of i.
        cntRight = 0
 
        # Traverse smaller characters
        # on left side of index i.
        for j in range(ord(strr[i]) - 1, -1, -1):
             
            # Update cntLeft
            cntLeft += cntLeftFreq[j]
 
            # Update cntRight
            cntRight += (cntFreq[j] -
                     cntLeftFreq[j])
 
        # Update cntLeftFreq[strr[i]]
        cntLeftFreq[ord(strr[i])] += 1
 
        # If count of smaller elements
        # on both sides equal
        if (cntLeft == cntRight and cntLeft != 0):
 
            # Print current index
            print(i, end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    strr = "aabacdabbb"
     
    printIndexes(strr)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find indexes
// of the given string that
// satisfy the condition
static void printIndexes(char[] str)
{
   
  // Stores length of
  // given string
  int N = str.Length;
 
  // Stores frequncy of
  // each character of str
  int []cntFreq = new int[256];
 
  for(int i = 0; i < N; i++)
  {
     
    // Update frequency of
    // current character
    cntFreq[str[i]]++;
  }
 
  // cntLeftFreq[i] Stores frequncy
  // of characters present on
  // the left side of index i.
  int []cntLeftFreq = new int[256];
 
  // Traverse the given string
  for(int i = 0; i < N; i++)
  {
     
    // Stores count of smaller
    // characters on left side of i.
    int cntLeft = 0;
 
    // Stores count of smaller
    // characters on Right side of i.
    int cntRight = 0;
 
    // Traverse smaller characters
    // on left side of index i.
    for(int j = str[i] - 1;
            j >= 0; j--)
    {
       
      // Update cntLeft  
      cntLeft += cntLeftFreq[j];
 
      // Update cntRight
      cntRight += cntFreq[j] -
                  cntLeftFreq[j];
    }
 
    // Update cntLeftFreq[str[i]]
    cntLeftFreq[str[i]]++;
 
    // If count of smaller elements
    // on both sides equal
    if (cntLeft == cntRight &&
        cntLeft != 0)
    {
       
      // Print current index;
      Console.Write(i + " ");
    }
  }
}
 
// Driver Code
public static void Main()
{
  string str = "aabacdabbb";
   
  printIndexes(str.ToCharArray());
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
2 4

时间复杂度: O(N * 256)
辅助空间: O(256)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live