Maximum distinct lowercase alphabets between two uppercase

Given a string containing alphabets in lowercase and uppercase, find the maximum count of distinct lowercase alphabets present between two uppercase alphabets.

Examples :

Input : zACaAbbaazzC
Output : The maximum count = 3

Input : edxedxxxCQiIVmYEUtLi
Output : The maximum count = 1

Method 1 (Using Character Count Array):

  • Declare an array of size 26 where each index of the array represents a character in English alphabet
  • Iterate the string over its complete length
  • For each lowercase character, increment the index of the corresponding array by 1.
  • For each uppercase character, iterate the array and count the number of positions having value greater than zero.
  • If this count is greater than the maximum count, update the maximum counter and initialize the array by 0.

Below is the implementation of the above method.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include <bits/stdc++.h>
using namespace std;
  
#define MAX_CHAR 26
  
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
int maxLower(string str)
{
    int n = str.length();
  
    // Ignoring lowercase characters in the
    // beginning.
    int i = 0;
    for (; i < n; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            i++;
            break;
        }
    }
  
    // We start from next of first capital letter
    // and traverse through remaining character.
    int maxCount = 0;
    int count[MAX_CHAR] = { 0 };
    for (; i < n; i++) {
  
        // If character is in uppercase,
        if (str[i] >= 'A' && str[i] <= 'Z') {
  
            // Count all distinct lower case
            // characters
            int currCount = 0;
            for (int j = 0; j < MAX_CHAR; j++)
                if (count[j] > 0)
                    currCount++;
  
            // Update maximum count
            maxCount = max(maxCount, currCount);
  
            // Reset count array
            memset(count, 0, sizeof(count));
        }
  
        // If character is in lowercase
        if (str[i] >= 'a' && str[i] <= 'z')
            count[str[i] - 'a']++;
    }
  
    return maxCount;
}
  
// Driver function
int main()
{
    string str = "zACaAbbaazzC";
    cout << maxLower(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
import java.util.Arrays;
  
class GFG 
{
  
    static final int MAX_CHAR = 26;
  
    // Function which computes the
    // maximum number of distinct
    // lowercase alphabets between
    // two uppercase alphabets
    static int maxLower(String str) 
    {
        int n = str.length();
  
        // Ignoring lowercase characters in the
        // beginning.
        int i = 0;
        for (; i < n; i++)
        {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
            {
                i++;
                break;
            }
        }
  
        // We start from next of first capital letter
        // and traverse through remaining character.
        int maxCount = 0;
        int count[] = new int[MAX_CHAR];
        for (; i < n; i++) 
        {
  
            // If character is in uppercase,
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'
            {
  
                // Count all distinct lower case
                // characters
                int currCount = 0;
                for (int j = 0; j < MAX_CHAR; j++) 
                {
                    if (count[j] > 0
                    {
                        currCount++;
                    }
                }
  
                // Update maximum count
                maxCount = Math.max(maxCount, currCount);
  
                // Reset count array
                Arrays.fill(count, 0);
            }
  
            // If character is in lowercase
            if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
            {
                count[str.charAt(i) - 'a']++;
            }
        }
        return maxCount;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        String str = "zACaAbbaazzC";
        System.out.println(maxLower(str));
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find maximum
# lowercase alphabets present
# between two uppercase alphabets
  
MAX_CHAR = 26
  
# Function which computes the
# maximum number of distinct
# lowercase alphabets between
# two uppercase alphabets
def maxLower(str):
    n = len(str)
  
    # Ignoring lowercase characters 
    # in the beginning.
    i = 0
    for i in range(n):
        if str[i] >= 'A' and str[i] <= 'Z':
            i += 1
            break
              
    # We start from next of first capital 
    # letter and traverse through 
    # remaining character.
    maxCount = 0
    count = []
    for j in range(MAX_CHAR):
        count.append(0)
          
    for j in range(i, n):
  
        # If character is in uppercase,
        if str[j] >= 'A' and str[j] <= 'Z':
  
            # Count all distinct lower
            # case characters
            currCount = 0
            for k in range(MAX_CHAR):
                if count[k] > 0:
                    currCount += 1
  
            # Update maximum count
            maxCount = max(maxCount, currCount)
  
            # Reset count array
            for y in count:
                y = 0
                  
        # If character is in lowercase
        if str[j] >= 'a' and str[j] <= 'z':
            count[ord(str[j]) - ord('a')] += 1
              
    return maxCount
  
# Driver function
str = "zACaAbbaazzC";
print(maxLower(str))
  
# This code is contributed by Upendra Bartwal

chevron_right


PHP

= ‘A’ &&
$str[$i] <= 'Z') { $i++; break; } } // We start from next of first capital letter // and traverse through remaining character. $maxCount = 0; $count = array_fill(0, $MAX_CHAR, NULL); for (; $i < $n; $i++) { // If character is in uppercase, if ($str[$i] >= ‘A’ && $str[$i] <= 'Z') { // Count all distinct lower case // characters $currCount = 0; for ($j = 0; $j < $MAX_CHAR; $j++) if ($count[$j] > 0)
$currCount++;

// Update maximum count
$maxCount = max($maxCount, $currCount);

// Reset count array
$count = array_fill(0, $MAX_CHAR, NULL);
}

// If character is in lowercase
if ($str[$i] >= ‘a’ && $str[$i] <= 'z') $count[ord($str[$i]) - ord('a')]++; } return $maxCount; } // Driver Code $str = "zACaAbbaazzC"; echo maxLower($str); // This code is contributed by ita_c ?>

Output:

3

Time Complexity : O(n).

 

Method 2 (Using Hash Table):In this method, we extensively use the C++ STL container unordered_set.

Below is the implementation of above method :

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include <bits/stdc++.h>
using namespace std;
  
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
int maxLower(string str)
{
    int n = str.length();
  
    // Ignoring lowercase characters in the
    // beginning.
    int i = 0;
    for (; i < n; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            i++;
            break;
        }
    }
  
    // We start from next of first capital letter
    // and traverse through remaining character.
    int maxCount = 0;
    unordered_set<int> s;
    for (; i < n; i++) {
  
        // If character is in uppercase,
        if (str[i] >= 'A' && str[i] <= 'Z') {
  
            // Update maximum count if lowercase
            // character before this is more.
            maxCount = max(maxCount, (int)s.size());
  
            // clear the set
            s.clear();
        }
  
        // If character is in lowercase
        if (str[i] >= 'a' && str[i] <= 'z')
            s.insert(str[i]);
    }
  
    return maxCount;
}
  
// Driver function
int main()
{
    string str = "zACaAbbaazzC";
    cout << maxLower(str);
    return 0;
}

chevron_right


Output:

3

Time complexity : O(n)



My Personal Notes arrow_drop_up

Waba Laba Dub Dub

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : aganjali10, Rajput-Ji, Ita_c



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.