📜  在字符串中找到最有价值的字母

📅  最后修改于: 2021-10-26 06:23:53             🧑  作者: Mango

给定一个字符串str ,任务是在str 中找到最大值的字母表。特定字母表的值定义为其最后一次出现和第一次出现的索引之间的差异。如果有多个这样的字母,那么找到字典序最小的字母。
例子:

方法:这个想法是将每个字母的第一个和最后一个出现在两个辅助数组中,比如first[]last[] 。现在,这两个数组可用于查找给定字符串的最大值字母表。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const int MAX = 26;
 
// Function to return the maximum
// valued alphabet
char maxAlpha(string str, int len)
{
 
    // To store the first and the last
    // occurrence of all the characters
    int first[MAX], last[MAX];
 
    // Set the first and the last occurrence
    // of all the characters to -1
    for (int i = 0; i < MAX; i++) {
        first[i] = -1;
        last[i] = -1;
    }
 
    // Update the occurrences of the characters
    for (int i = 0; i < len; i++) {
 
        int index = (str[i] - 'a');
 
        // Only set the first occurrence if
        // it hasn't already been set
        if (first[index] == -1)
            first[index] = i;
 
        last[index] = i;
    }
 
    // To store the result
    int ans = -1, maxVal = -1;
 
    // For every alphabet
    for (int i = 0; i < MAX; i++) {
 
        // If current alphabet doesn't appear
        // in the given string
        if (first[i] == -1)
            continue;
 
        // If the current character has
        // the highest value so far
        if ((last[i] - first[i]) > maxVal) {
            maxVal = last[i] - first[i];
            ans = i;
        }
    }
 
    return (char)(ans + 'a');
}
 
// Driver code
int main()
{
    string str = "abbba";
    int len = str.length();
 
    cout << maxAlpha(str, len);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
static int MAX = 26;
 
// Function to return the maximum
// valued alphabet
static char maxAlpha(String str, int len)
{
 
    // To store the first and the last
    // occurrence of all the characters
    int []first = new int[MAX];
    int []last = new int[MAX];
 
    // Set the first and the last occurrence
    // of all the characters to -1
    for (int i = 0; i < MAX; i++)
    {
        first[i] = -1;
        last[i] = -1;
    }
 
    // Update the occurrences of the characters
    for (int i = 0; i < len; i++)
    {
 
        int index = (str.charAt(i) - 'a');
 
        // Only set the first occurrence if
        // it hasn't already been set
        if (first[index] == -1)
            first[index] = i;
 
        last[index] = i;
    }
 
    // To store the result
    int ans = -1, maxVal = -1;
 
    // For every alphabet
    for (int i = 0; i < MAX; i++)
    {
 
        // If current alphabet doesn't appear
        // in the given String
        if (first[i] == -1)
            continue;
 
        // If the current character has
        // the highest value so far
        if ((last[i] - first[i]) > maxVal)
        {
            maxVal = last[i] - first[i];
            ans = i;
        }
    }
    return (char)(ans + 'a');
}
 
// Driver code
public static void main(String[] args)
{
    String str = "abbba";
    int len = str.length();
 
    System.out.print(maxAlpha(str, len));
}
}
 
// This code is contributed by 29AjayKumar


Python
# Python implementation of the approach
MAX = 26
 
# Function to return the maximum
# valued alphabet
def maxAlpha(str, len):
 
    # To store the first and the last
    # occurrence of all the characters
 
    # Set the first and the last occurrence
    # of all the characters to -1
     
    first = [-1 for x in range(MAX)]
    last = [-1 for x in range(MAX)]
     
    # Update the occurrences of the characters
    for i in range(0,len):
 
        index = ord(str[i])-97
 
        # Only set the first occurrence if
        # it hasn't already been set
        if (first[index] == -1):
            first[index] = i
 
        last[index] = i
     
    # To store the result
    ans = -1
    maxVal = -1
 
    # For every alphabet
    for i in range(0,MAX):
 
        # If current alphabet doesn't appear
        # in the given string
        if (first[i] == -1):
            continue
 
        # If the current character has
        # the highest value so far
        if ((last[i] - first[i]) > maxVal):
            maxVal = last[i] - first[i];
            ans = i
 
    return chr(ans + 97)
 
# Driver code
str = "abbba"
len = len(str)
 
print(maxAlpha(str, len))
 
# This code is contributed by Sanjit_Prasad


C#
// C# implementation of the approach
using System;
 
class GFG
{
static int MAX = 26;
 
// Function to return the maximum
// valued alphabet
static char maxAlpha(String str, int len)
{
 
    // To store the first and the last
    // occurrence of all the characters
    int []first = new int[MAX];
    int []last = new int[MAX];
 
    // Set the first and the last occurrence
    // of all the characters to -1
    for (int i = 0; i < MAX; i++)
    {
        first[i] = -1;
        last[i] = -1;
    }
 
    // Update the occurrences of the characters
    for (int i = 0; i < len; i++)
    {
 
        int index = (str[i] - 'a');
 
        // Only set the first occurrence if
        // it hasn't already been set
        if (first[index] == -1)
            first[index] = i;
 
        last[index] = i;
    }
 
    // To store the result
    int ans = -1, maxVal = -1;
 
    // For every alphabet
    for (int i = 0; i < MAX; i++)
    {
 
        // If current alphabet doesn't appear
        // in the given String
        if (first[i] == -1)
            continue;
 
        // If the current character has
        // the highest value so far
        if ((last[i] - first[i]) > maxVal)
        {
            maxVal = last[i] - first[i];
            ans = i;
        }
    }
    return (char)(ans + 'a');
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "abbba";
    int len = str.Length;
 
    Console.Write(maxAlpha(str, len));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

a

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程