📌  相关文章
📜  字符串右侧较大元素的数量

📅  最后修改于: 2021-04-29 10:38:31             🧑  作者: Mango

给定一个字符串,请为该字符串的每个字符查找较大字母的数量计数。

例子:

Input  : str = "abcd"
Output : 3 2 1 0
There are 3 greater characters on right of 'a',
2 greater characters on right of 'b', 1 greater
character on right of 'c' and 0 greater characters
on right of 'd'.
      
Input :  geeks
Output : 2 2 2 1 0

天真的方法是使用两个for循环。首先将跟踪字符串的每个字母,第二个循环将根据ASCII值查找更大的字母。

C++
// CPP program to find counts of right greater
// characters for every character.
#include 
using namespace std;
  
void printGreaterCount(string str)
{
    int len = str.length(), right[len] = { 0 };
    for (int i = 0; i < len; i++)
        for (int j = i + 1; j < len; j++)
            if (str[i] < str[j])
                right[i]++;
  
    for (int i = 0; i < len; i++)
        cout << right[i] << " ";
}
  
// Driver code
int main()
{
    string str = "abcd";
    printGreaterCount(str);
    return 0;
}


Java
// Java program to find counts of right greater
// characters for every character.
class GFG {
  
    static void printGreaterCount(String str)
    {
        int len = str.length(), right[] = new int[len];
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (str.charAt(i) < str.charAt(j)) {
                    right[i]++;
                }
            }
        }
  
        for (int i = 0; i < len; i++) {
            System.out.print(right[i] + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str = "abcd";
        printGreaterCount(str);
    }
}
// This code is contributed Rajput-Ji


Python3
# Python3 program to find counts of right 
# greater characters for every character.
  
def printGreaterCount(str):
    len__ = len(str)
    right = [0 for i in range(len__)] 
    for i in range(len__):
        for j in range(i + 1, len__, 1):
            if (str[i] < str[j]):
                right[i] += 1
  
    for i in range(len__):
        print(right[i], end = " ")
  
# Driver code
if __name__ == '__main__':
    str = "abcd"
    printGreaterCount(str)
      
# This code is contributed by
# Sahil_Shelangia


C#
// C# program to find counts of right greater
// characters for every character.
using System;
public class GFG {
  
    static void printGreaterCount(String str)
    {
        int len = str.Length;
        int[] right = new int[len];
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (str[i] < str[j]) {
                    right[i]++;
                }
            }
        }
  
        for (int i = 0; i < len; i++) {
            Console.Write(right[i] + " ");
        }
    }
  
    // Driver code
    public static void Main()
    {
        String str = "abcd";
        printGreaterCount(str);
    }
}
// This code is contributed by 29AjayKumar


PHP


C++
// C++ program to count greater characters on right
// side of every character.
#include 
using namespace std;
#define MAX_CHAR 26
  
void printGreaterCount(string str)
{
    int len = str.length();
  
    // Arrays to store result and character counts.
    int ans[len] = { 0 }, count[MAX_CHAR] = { 0 };
  
    // start from right side of string
    for (int i = len - 1; i >= 0; i--) {
        count[str[i] - 'a']++;
        for (int j = str[i] - 'a' + 1; j < MAX_CHAR; j++)
            ans[i] += count[j];
    }
  
    for (int i = 0; i < len; i++)
        cout << ans[i] << " ";
}
  
// Driver code
int main()
{
    string str = "abcd";
    printGreaterCount(str);
    return 0;
}


Java
// Java program to count greater characters on right
// side of every character.
public class GFG {
  
    final static int MAX_CHAR = 26;
  
    static void printGreaterCount(String str)
    {
        int len = str.length();
  
        // Arrays to store result and character counts.
        int ans[] = new int[len], count[] = new int[MAX_CHAR];
  
        // start from right side of string
        for (int i = len - 1; i >= 0; i--) {
            count[str.charAt(i) - 'a']++;
            for (int j = str.charAt(i) - 'a' + 1; j < MAX_CHAR; j++) {
                ans[i] += count[j];
            }
        }
  
        for (int i = 0; i < len; i++) {
            System.out.print(ans[i] + " ");
        }
    }
  
    // Driver code
    static public void main(String[] args)
    {
        String str = "abcd";
        printGreaterCount(str);
    }
}


Python3
# Python3 program to count greater characters on right
# side of every character.
  
MAX_CHAR=26;
  
def printGreaterCount(str1):
      
    len1 = len(str1);
  
    # Arrays to store result and character counts.
    ans=[0]*len1;
    count=[0]*MAX_CHAR;
  
    # start from right side of string
    for i in range(len1 - 1,-1,-1):
        count[ord(str1[i]) - ord('a')]+=1;
        for j in range(ord(str1[i]) - ord('a') + 1,MAX_CHAR):
            ans[i] += count[j];
  
    for i in range(len1):
        print(ans[i],end=" ");
  
# Driver code
str1 = "abcd";
printGreaterCount(str1);
  
# This code is contributed by mits


C#
// C# program to count greater characters on right
// side of every character.
using System;
  
class GFG {
  
    static int MAX_CHAR = 26;
  
    static void printGreaterCount(string str)
    {
        int len = str.Length;
  
        // Arrays to store result and character counts.
        int[] ans = new int[len];
        int[] count = new int[MAX_CHAR];
  
        // start from right side of string
        for (int i = len - 1; i >= 0; i--) {
            count[str[i] - 'a']++;
            for (int j = str[i] - 'a' + 1; j < MAX_CHAR; j++) {
                ans[i] += count[j];
            }
        }
  
        for (int i = 0; i < len; i++) {
            Console.Write(ans[i] + " ");
        }
    }
  
    // Driver code
    static void Main()
    {
        string str = "abcd";
        printGreaterCount(str);
    }
}
  
// This code is contributed by mits


PHP
= 0; $i--) 
    {
        $count[ord($str[$i]) - ord('a')]++;
        for ($j = ord($str[$i]) - ord('a') + 1; $j < $MAX_CHAR; $j++)
            $ans[$i] += $count[$j];
    }
  
    for ($i = 0; $i < $len; $i++)
        echo $ans[$i]." ";
}
  
    // Driver code
    $str = "abcd";
    printGreaterCount($str);
  
// This code is contributed by mits
?>


输出:
3 2 1 0

时间复杂度: O(N * N)

一种有效的方法是从右侧遍历字符串,并从右侧跟踪字符计数。对于我们从右遍历的每个字符,我们在count数组中增加其计数,并添加所有更大字符的计数以回答该字符。

C++

// C++ program to count greater characters on right
// side of every character.
#include 
using namespace std;
#define MAX_CHAR 26
  
void printGreaterCount(string str)
{
    int len = str.length();
  
    // Arrays to store result and character counts.
    int ans[len] = { 0 }, count[MAX_CHAR] = { 0 };
  
    // start from right side of string
    for (int i = len - 1; i >= 0; i--) {
        count[str[i] - 'a']++;
        for (int j = str[i] - 'a' + 1; j < MAX_CHAR; j++)
            ans[i] += count[j];
    }
  
    for (int i = 0; i < len; i++)
        cout << ans[i] << " ";
}
  
// Driver code
int main()
{
    string str = "abcd";
    printGreaterCount(str);
    return 0;
}

Java

// Java program to count greater characters on right
// side of every character.
public class GFG {
  
    final static int MAX_CHAR = 26;
  
    static void printGreaterCount(String str)
    {
        int len = str.length();
  
        // Arrays to store result and character counts.
        int ans[] = new int[len], count[] = new int[MAX_CHAR];
  
        // start from right side of string
        for (int i = len - 1; i >= 0; i--) {
            count[str.charAt(i) - 'a']++;
            for (int j = str.charAt(i) - 'a' + 1; j < MAX_CHAR; j++) {
                ans[i] += count[j];
            }
        }
  
        for (int i = 0; i < len; i++) {
            System.out.print(ans[i] + " ");
        }
    }
  
    // Driver code
    static public void main(String[] args)
    {
        String str = "abcd";
        printGreaterCount(str);
    }
}

Python3

# Python3 program to count greater characters on right
# side of every character.
  
MAX_CHAR=26;
  
def printGreaterCount(str1):
      
    len1 = len(str1);
  
    # Arrays to store result and character counts.
    ans=[0]*len1;
    count=[0]*MAX_CHAR;
  
    # start from right side of string
    for i in range(len1 - 1,-1,-1):
        count[ord(str1[i]) - ord('a')]+=1;
        for j in range(ord(str1[i]) - ord('a') + 1,MAX_CHAR):
            ans[i] += count[j];
  
    for i in range(len1):
        print(ans[i],end=" ");
  
# Driver code
str1 = "abcd";
printGreaterCount(str1);
  
# This code is contributed by mits

C#

// C# program to count greater characters on right
// side of every character.
using System;
  
class GFG {
  
    static int MAX_CHAR = 26;
  
    static void printGreaterCount(string str)
    {
        int len = str.Length;
  
        // Arrays to store result and character counts.
        int[] ans = new int[len];
        int[] count = new int[MAX_CHAR];
  
        // start from right side of string
        for (int i = len - 1; i >= 0; i--) {
            count[str[i] - 'a']++;
            for (int j = str[i] - 'a' + 1; j < MAX_CHAR; j++) {
                ans[i] += count[j];
            }
        }
  
        for (int i = 0; i < len; i++) {
            Console.Write(ans[i] + " ");
        }
    }
  
    // Driver code
    static void Main()
    {
        string str = "abcd";
        printGreaterCount(str);
    }
}
  
// This code is contributed by mits

的PHP

= 0; $i--) 
    {
        $count[ord($str[$i]) - ord('a')]++;
        for ($j = ord($str[$i]) - ord('a') + 1; $j < $MAX_CHAR; $j++)
            $ans[$i] += $count[$j];
    }
  
    for ($i = 0; $i < $len; $i++)
        echo $ans[$i]." ";
}
  
    // Driver code
    $str = "abcd";
    printGreaterCount($str);
  
// This code is contributed by mits
?>
输出:
3 2 1 0

时间复杂度: O(N)