给定一个仅由小写英文字母组成的字符串。任务是计算每个索引处字符右侧按字母顺序排列的较小字符的总数。
例子:
Input: str = “edcba”
Output: 4 3 2 1 0
Explanation:
The number of characters on the right side of index 0 which is smaller than
e are dcba = 4
The number of characters on the right side of index 1 which is smaller than
d are cba = 3
The number of characters on the right side of index 2 which is smaller than
c are ba = 2
The number of characters on the right side of index 3 which is smaller than
b are a = 1
The number of characters on the right side of index 4 which is smaller than
a are ‘\0’ = 0
Input: str = “eaaa”
Output: 3 0 0 0
天真的方法:
这个想法是一个一个地遍历字符串每个索引右侧的所有字符,并打印按字母顺序排列较小的字符数。
C++
#include
using namespace std;
// function to count the smaller
// characters at the right of index i
void countSmaller(string str)
{
// store the length of string
int n = str.length();
for (int i = 0; i < n; i++) {
// for each index initialize
// count as zero
int cnt = 0;
for (int j = i + 1; j < n; j++) {
// increment the count if
// characters are smaller
// than at ith index
if (str[j] < str[i]) {
cnt += 1;
}
}
// print the count of characters
// smaller than the index i
cout << cnt << " ";
}
}
// Driver code
int main()
{
// input string
string str = "edcba";
countSmaller(str);
}
Java
class GFG
{
// function to count the smaller
// characters at the right of index i
static void countSmaller(String str)
{
// store the length of String
int n = str.length();
for (int i = 0; i < n; i++)
{
// for each index initialize
// count as zero
int cnt = 0;
for (int j = i + 1; j < n; j++)
{
// increment the count if
// characters are smaller
// than at ith index
if (str.charAt(j) < str.charAt(i))
{
cnt += 1;
}
}
// print the count of characters
// smaller than the index i
System.out.print(cnt+ " ");
}
}
// Driver code
public static void main(String[] args)
{
// input String
String str = "edcba";
countSmaller(str);
}
}
// This code is contributed by 29AjayKumar
Python3
# function to count the smaller
# characters at the right of index i
def countSmaller(str):
# store the length of String
n = len(str);
for i in range(n):
# for each index initialize
# count as zero
cnt = 0;
for j in range(i + 1, n):
# increment the count if
# characters are smaller
# than at ith index
if (str[j] < str[i]):
cnt += 1;
# print the count of characters
# smaller than the index i
print(cnt, end =" ");
# Driver code
if __name__ == '__main__':
# input String
str = "edcba";
countSmaller(str);
# This code is contributed by PrinciRaj1992
C#
using System;
class GFG
{
// function to count the smaller
// characters at the right of index i
static void countSmaller(String str)
{
// store the length of String
int n = str.Length;
for (int i = 0; i < n; i++)
{
// for each index initialize
// count as zero
int cnt = 0;
for (int j = i + 1; j < n; j++)
{
// increment the count if
// characters are smaller
// than at ith index
if (str[j] < str[i])
{
cnt += 1;
}
}
// print the count of characters
// smaller than the index i
Console.Write(cnt+ " ");
}
}
// Driver code
public static void Main(String[] args)
{
// input String
String str = "edcba";
countSmaller(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
#include
using namespace std;
// Function to count the smaller
// characters on the right of index i
void countSmaller(string str)
{
// store the length of string
int n = str.length();
// initialize each elements of
// arr to zero
int arr[26] = { 0 };
// array to store count of smaller characters on
// the right side of that index
int ans[n];
for (int i = n - 1; i >= 0; i--) {
arr[str[i] - 'a']++;
// initialize the variable to store
// the count of characters smaller
// than that at index i
int ct = 0;
// adding the count of characters
// smaller than index i
for (int j = 0; j < str[i] - 'a'; j++) {
ct += arr[j];
}
ans[i] = ct;
}
// print the count of characters smaller
// than index i stored in ans array
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
string str = "edcbaa";
countSmaller(str);
return 0;
}
Java
class GFG
{
// Function to count the smaller
// characters on the right of index i
static void countSmaller(String str)
{
// store the length of String
int n = str.length();
// initialize each elements of
// arr to zero
int arr[] = new int[26];
// array to store count of smaller characters on
// the right side of that index
int ans[] = new int[n];
for (int i = n - 1; i >= 0; i--)
{
arr[str.charAt(i) - 'a']++;
// initialize the variable to store
// the count of characters smaller
// than that at index i
int ct = 0;
// adding the count of characters
// smaller than index i
for (int j = 0; j < str.charAt(i) - 'a'; j++)
{
ct += arr[j];
}
ans[i] = ct;
}
// print the count of characters smaller
// than index i stored in ans array
for (int i = 0; i < n; i++)
{
System.out.print(ans[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
String str = "edcbaa";
countSmaller(str);
}
}
// This code is contributed by 29AjayKumar
Python3
# Function to count the smaller
# characters on the right of index i
def countSmaller(str):
# store the length of String
n = len(str);
# initialize each elements of
# arr to zero
arr = [0]*26;
# array to store count of smaller characters on
# the right side of that index
ans = [0]*n;
for i in range(n - 1, -1, -1):
arr[ord(str[i] ) - ord('a')] += 1;
# initialize the variable to store
# the count of characters smaller
# than that at index i
ct = 0;
# adding the count of characters
# smaller than index i
for j in range(ord(str[i] ) - ord('a')):
ct += arr[j];
ans[i] = ct;
# print the count of characters smaller
# than index i stored in ans array
for i in range(n):
print(ans[i], end = " ");
# Driver Code
if __name__ == '__main__':
str = "edcbaa";
countSmaller(str);
# This code is contributed by Rajput-Ji
C#
using System;
class GFG
{
// Function to count the smaller
// characters on the right of index i
static void countSmaller(String str)
{
// store the length of String
int n = str.Length;
// initialize each elements of
// arr to zero
int []arr = new int[26];
// array to store count of smaller characters on
// the right side of that index
int []ans = new int[n];
for (int i = n - 1; i >= 0; i--)
{
arr[str[i] - 'a']++;
// initialize the variable to store
// the count of characters smaller
// than that at index i
int ct = 0;
// adding the count of characters
// smaller than index i
for (int j = 0; j < str[i] - 'a'; j++)
{
ct += arr[j];
}
ans[i] = ct;
}
// print the count of characters smaller
// than index i stored in ans array
for (int i = 0; i < n; i++)
{
Console.Write(ans[i] + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
String str = "edcbaa";
countSmaller(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
4 3 2 1 0
时间复杂度: O( N 2 ),其中 N =字符串长度
辅助空间: O(1)
更好的方法:这个想法是使用自平衡 BST。
自平衡二叉搜索树(AVL、Red Black、.. 等)可用于获得 O(N log N) 时间复杂度的解决方案。我们可以扩充这些树,以便每个节点 N 都包含以 N 为根的子树的大小。我们在以下实现中使用了 AVL 树。
我们从右到左遍历字符串,将所有元素一一插入到一棵 AVL 树中。在 AVL 树中插入新密钥时,我们首先将密钥与根进行比较。如果键大于根,则它大于根左子树中的所有节点。因此,我们将左子树的大小添加到要插入的键的较小元素的计数中。我们对根下的所有节点递归地遵循相同的方法。
上述方法的实现请参考这篇文章。
时间复杂度: O( N*log N )
辅助空间: O( N )
有效的方法:
这个想法是使用散列技术,因为字符串仅由小写字母组成。所以这里我们可以使用一个大小为26的数组,用于存储该索引右侧的较小字符的计数。
从右侧遍历数组并不断更新哈希数组中的字符数。现在,为了找到右边较小字符的数量,我们可以简单地每次遍历大小为 26 的哈希数组来计算到目前为止遇到的较小字符的数量。
C++
#include
using namespace std;
// Function to count the smaller
// characters on the right of index i
void countSmaller(string str)
{
// store the length of string
int n = str.length();
// initialize each elements of
// arr to zero
int arr[26] = { 0 };
// array to store count of smaller characters on
// the right side of that index
int ans[n];
for (int i = n - 1; i >= 0; i--) {
arr[str[i] - 'a']++;
// initialize the variable to store
// the count of characters smaller
// than that at index i
int ct = 0;
// adding the count of characters
// smaller than index i
for (int j = 0; j < str[i] - 'a'; j++) {
ct += arr[j];
}
ans[i] = ct;
}
// print the count of characters smaller
// than index i stored in ans array
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
string str = "edcbaa";
countSmaller(str);
return 0;
}
Java
class GFG
{
// Function to count the smaller
// characters on the right of index i
static void countSmaller(String str)
{
// store the length of String
int n = str.length();
// initialize each elements of
// arr to zero
int arr[] = new int[26];
// array to store count of smaller characters on
// the right side of that index
int ans[] = new int[n];
for (int i = n - 1; i >= 0; i--)
{
arr[str.charAt(i) - 'a']++;
// initialize the variable to store
// the count of characters smaller
// than that at index i
int ct = 0;
// adding the count of characters
// smaller than index i
for (int j = 0; j < str.charAt(i) - 'a'; j++)
{
ct += arr[j];
}
ans[i] = ct;
}
// print the count of characters smaller
// than index i stored in ans array
for (int i = 0; i < n; i++)
{
System.out.print(ans[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
String str = "edcbaa";
countSmaller(str);
}
}
// This code is contributed by 29AjayKumar
蟒蛇3
# Function to count the smaller
# characters on the right of index i
def countSmaller(str):
# store the length of String
n = len(str);
# initialize each elements of
# arr to zero
arr = [0]*26;
# array to store count of smaller characters on
# the right side of that index
ans = [0]*n;
for i in range(n - 1, -1, -1):
arr[ord(str[i] ) - ord('a')] += 1;
# initialize the variable to store
# the count of characters smaller
# than that at index i
ct = 0;
# adding the count of characters
# smaller than index i
for j in range(ord(str[i] ) - ord('a')):
ct += arr[j];
ans[i] = ct;
# print the count of characters smaller
# than index i stored in ans array
for i in range(n):
print(ans[i], end = " ");
# Driver Code
if __name__ == '__main__':
str = "edcbaa";
countSmaller(str);
# This code is contributed by Rajput-Ji
C#
using System;
class GFG
{
// Function to count the smaller
// characters on the right of index i
static void countSmaller(String str)
{
// store the length of String
int n = str.Length;
// initialize each elements of
// arr to zero
int []arr = new int[26];
// array to store count of smaller characters on
// the right side of that index
int []ans = new int[n];
for (int i = n - 1; i >= 0; i--)
{
arr[str[i] - 'a']++;
// initialize the variable to store
// the count of characters smaller
// than that at index i
int ct = 0;
// adding the count of characters
// smaller than index i
for (int j = 0; j < str[i] - 'a'; j++)
{
ct += arr[j];
}
ans[i] = ct;
}
// print the count of characters smaller
// than index i stored in ans array
for (int i = 0; i < n; i++)
{
Console.Write(ans[i] + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
String str = "edcbaa";
countSmaller(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
5 4 3 2 0 0
时间复杂度: O(N*26)
辅助空间: O(N+26)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live