给定字符串str ,任务是找到给定字符串的索引,以使该索引左侧和右侧的按字典顺序排列的较小字符数相等且非零。
例子:
Input: str = “aabacdabbb”
Output: 2 4
Explanation:
Count of smaller characters on the left side of index 2 is 2 and right side of index 2 is also 2.
Count of smaller characters on the left side of index 4 is 4 and right side of index 4 is also 4.
Therefore, the required output is 2 4.
Input: “geeksforgeeks”
Output: 5
Explanation:
Count of smaller characters on the left side of index 5 is 2 and right side of index 5 is also 2.
Therefore, the required output is 5.
原始的方法:要解决这个问题最简单的办法是遍历定的字符串,依靠左侧和右侧给定的字符串的各指标的字典序小字符数。对于每个索引,请检查当前索引左侧和右侧在字典上较小的字符数是否相等。如果发现为真,则打印当前索引。
时间复杂度: (N 2 )
辅助空间: O(1)
高效的方法:为了优化上述方法,其思想是使用哈希。请按照以下步骤解决问题:
- 初始化一个数组,比如说cntFre []来存储给定字符串的每个字符的频率
- 遍历给定的字符串和存储给定字符串的每个字符的频率。
- 初始化一个数组,例如cntLeftFreq [],以存储出现在给定字符串当前索引左侧的所有字符的频率。
- 遍历给定的字符串,并存储出现在当前索引左侧的所有按字典顺序排列的较小字符的频率。
- 对于每个索引,请检查当前索引左侧和右侧在字典上较小的字符数是否相等。如果发现为true,则打印给定字符串的当前索引。
下面是上述方法的实现:
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
2 4
时间复杂度: O(N * 256)
辅助空间: O(256)