📌  相关文章
📜  查找字符串存在的最大字母字符

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

给定一个字符串str ,我们的任务是找到最大的字母字符,它的大写和小写都出现在字符串。应返回大写字符。如果没有这样的字符,则返回 -1 否则打印字符的大写字母。

例子:

朴素的方法:为了解决上述问题,朴素的方法是检查字符串中每个字符是否存在大写或小写字符,即对于字母 A ‘a’ 和 ‘A’ 都应该在字符串。如果存在这样的字母,则将跟踪该字符并仅在当前字符大于先前选择的字符。该方法在最坏情况下需要 O(n 2 ) 时间,并且可以进一步优化。

有效的方法:为了优化上述方法,我们使用英文字母表中有 26 个字符的事实,我们可以通过维护大小为 26 的数组来跟踪小写和大写字符。在迭代给定的字符串,我们将标记为true如果当前字符同时存在大写和小写字母,则在两个数组中。在标记相应数组中存在的所有字符,我们将运行从 25 到 0 的循环并检查两个数组中是否都标记了“真”。如果是,则打印该字符的大写字母,否则返回 -1。

下面是上述方法的实现:

Java
// Java program to Find the Largest Alphabetic
// Character present in the string of both
// uppercase and lowercase English characters
 
public class Main {
 
    // Function to find the Largest Alphabetic Character
    public static String largestCharacter(String str)
    {
        // Array for keeping track of both uppercase
        // and lowercase english alphabets
        boolean[] uppercase = new boolean[26];
        boolean[] lowercase = new boolean[26];
 
        char[] arr = str.toCharArray();
 
        for (char c : arr) {
 
            if (Character.isLowerCase(c))
                lowercase = true;
 
            if (Character.isUpperCase(c))
                uppercase = true;
        }
 
        // Iterate from right side of array
        // to get the largest index character
        for (int i = 25; i >= 0; i--) {
 
            // Check for the character if both its
            // uppercase and lowercase exist or not
            if (uppercase[i] && lowercase[i])
                return (char)(i + 'A') + "";
        }
 
        // Return -1 if no such character whose
        // uppercase and lowercase present in
        // string str
        return "-1";
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "admeDCAB";
 
        System.out.println(largestCharacter(str));
    }
}


C#
// C# program to Find the Largest Alphabetic
// char present in the string of both
// uppercase and lowercase English characters
using System;
 
class GFG{
   
// Function to find the Largest Alphabetic char
public static String largestchar(String str)
{
     
    // Array for keeping track of both uppercase
    // and lowercase english alphabets
    bool[] uppercase = new bool[26];
    bool[] lowercase = new bool[26];
 
    char[] arr = str.ToCharArray();
 
    foreach(char c in arr)
    {
        if (char.IsLower(c))
            lowercase = true;
 
        if (char.IsUpper(c))
             uppercase = true;
    }
 
    // Iterate from right side of array
    // to get the largest index character
    for(int i = 25; i >= 0; i--)
    {
         
        // Check for the character if both its
        // uppercase and lowercase exist or not
        if (uppercase[i] && lowercase[i])
            return (char)(i + 'A') + "";
    }
 
    // Return -1 if no such character whose
    // uppercase and lowercase present in
    // string str
    return "-1";
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "admeDCAB";
 
    Console.WriteLine(largestchar(str));
}
}
  
// This code is contributed by amal kumar choubey


输出:

D

时间复杂度: O(n) 其中 n 是字符串 的长度。
空间复杂度: O(52)

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