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 the 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++
#include <bits/stdc++.h>
using namespace std;
#define MAX_CHAR 26
int maxLower(string str)
{
int n = str.length();
int i = 0;
for (; i < n; i++) {
if (str[i] >= 'A' && str[i] <= 'Z' ) {
i++;
break ;
}
}
int maxCount = 0;
int count[MAX_CHAR] = { 0 };
for (; i < n; i++) {
if (str[i] >= 'A' && str[i] <= 'Z' ) {
int currCount = 0;
for ( int j = 0; j < MAX_CHAR; j++)
if (count[j] > 0)
currCount++;
maxCount = max(maxCount, currCount);
memset (count, 0, sizeof (count));
}
if (str[i] >= 'a' && str[i] <= 'z' )
count[str[i] - 'a' ]++;
}
return maxCount;
}
int main()
{
string str = "zACaAbbaazzC" ;
cout << maxLower(str);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static final int MAX_CHAR = 26 ;
static int maxLower(String str)
{
int n = str.length();
int i = 0 ;
for (; i < n; i++)
{
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z' )
{
i++;
break ;
}
}
int maxCount = 0 ;
int count[] = new int [MAX_CHAR];
for (; i < n; i++)
{
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z' )
{
int currCount = 0 ;
for ( int j = 0 ; j < MAX_CHAR; j++)
{
if (count[j] > 0 )
{
currCount++;
}
}
maxCount = Math.max(maxCount, currCount);
Arrays.fill(count, 0 );
}
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' )
{
count[str.charAt(i) - 'a' ]++;
}
}
return maxCount;
}
public static void main(String[] args)
{
String str = "zACaAbbaazzC" ;
System.out.println(maxLower(str));
}
}
|
Python3
MAX_CHAR = 26
def maxLower( str ):
n = len ( str )
i = 0
for i in range (n):
if str [i] > = 'A' and str [i] < = 'Z' :
i + = 1
break
maxCount = 0
count = []
for j in range (MAX_CHAR):
count.append( 0 )
for j in range (i, n):
if str [j] > = 'A' and str [j] < = 'Z' :
currCount = 0
for k in range (MAX_CHAR):
if count[k] > 0 :
currCount + = 1
maxCount = max (maxCount, currCount)
for y in count:
y = 0
if str [j] > = 'a' and str [j] < = 'z' :
count[ ord ( str [j]) - ord ( 'a' )] + = 1
return maxCount
str = "zACaAbbaazzC" ;
print (maxLower( str ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX_CHAR = 26;
static int maxLower(String str)
{
int n = str.Length;
int i = 0;
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
i++;
break ;
}
}
int maxCount = 0;
int []count = new int [MAX_CHAR];
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
int currCount = 0;
for ( int j = 0; j < MAX_CHAR; j++)
{
if (count[j] > 0)
{
currCount++;
}
}
maxCount = Math.Max(maxCount, currCount);
Array.Fill(count, 0);
}
if (str[i] >= 'a' && str[i] <= 'z' )
{
count[str[i] - 'a' ]++;
}
}
return maxCount;
}
public static void Main(String[] args)
{
String str = "zACaAbbaazzC" ;
Console.WriteLine(maxLower(str));
}
}
|
PHP
<?php
$MAX_CHAR = 26;
function maxLower( $str )
{
global $MAX_CHAR ;
$n = strlen ( $str );
$i = 0;
for (; $i < $n ; $i ++)
{
if ( $str [ $i ] >= 'A' &&
$str [ $i ] <= 'Z' )
{
$i ++;
break ;
}
}
$maxCount = 0;
$count = array_fill (0, $MAX_CHAR , NULL);
for (; $i < $n ; $i ++)
{
if ( $str [ $i ] >= 'A' && $str [ $i ] <= 'Z' )
{
$currCount = 0;
for ( $j = 0; $j < $MAX_CHAR ; $j ++)
if ( $count [ $j ] > 0)
$currCount ++;
$maxCount = max( $maxCount , $currCount );
$count = array_fill (0, $MAX_CHAR , NULL);
}
if ( $str [ $i ] >= 'a' && $str [ $i ] <= 'z' )
$count [ord( $str [ $i ]) - ord( 'a' )]++;
}
return $maxCount ;
}
$str = "zACaAbbaazzC" ;
echo maxLower( $str );
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function maxLower(str)
{
let n = str.length;
let i = 0;
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
i++;
break ;
}
}
let maxCount = 0;
let count = new Array(MAX_CHAR);
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
let currCount = 0;
for (let j = 0; j < MAX_CHAR; j++)
{
if (count[j] > 0)
{
currCount++;
}
}
maxCount = Math.max(maxCount, currCount);
for (let i=0;i<count.length;i++)
{
count[i]=0;
}
}
if (str[i] >= 'a' && str[i] <= 'z' )
{
count[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
}
return maxCount;
}
let str = "zACaAbbaazzC" ;
document.write(maxLower(str));
</script>
|
Time Complexity: O(n).
Auxiliary Space: O(1).
Method 2 (Using Hash Table): In this method, we extensively use the C++ STL container unordered_set.
Below is the implementation of the above method :
C++
#include <bits/stdc++.h>
using namespace std;
int maxLower(string str)
{
int n = str.length();
int i = 0;
for (; i < n; i++) {
if (str[i] >= 'A' && str[i] <= 'Z' ) {
i++;
break ;
}
}
int maxCount = 0;
unordered_set< int > s;
for (; i < n; i++) {
if (str[i] >= 'A' && str[i] <= 'Z' ) {
maxCount = max(maxCount, ( int )s.size());
s.clear();
}
if (str[i] >= 'a' && str[i] <= 'z' )
s.insert(str[i]);
}
return maxCount;
}
int main()
{
string str = "zACaAbbaazzC" ;
cout << maxLower(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxLower( char [] str)
{
int n = str.length;
int i = 0 ;
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
i++;
break ;
}
}
int maxCount = 0 ;
HashSet<Integer> s = new HashSet<Integer>();
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
maxCount = Math.max(maxCount, ( int )s.size());
s.clear();
}
if (str[i] >= 'a' && str[i] <= 'z' )
s.add(( int )str[i]);
}
return maxCount;
}
public static void main(String args[])
{
String str = "zACaAbbaazzC" ;
System.out.println(maxLower(str.toCharArray()));
}
}
|
Python3
def maxLower( str ):
n = len ( str );
i = 0 ;
for i in range (n):
if ( str [i] > = 'A' and
str [i] < = 'Z' ):
i + = 1 ;
break ;
maxCount = 3 ;
s = set ()
for i in range (n):
if ( str [i] > = 'A' and
str [i] < = 'Z' ):
maxCount = max (maxCount,
len (s));
s.clear();
if ( str [i] > = 'a' and
str [i] < = 'z' ):
s.add( str [i]);
return maxCount;
if __name__ = = '__main__' :
str = "zACaAbbaazzC" ;
print (maxLower( str ));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxLower( char [] str)
{
int n = str.Length;
int i = 0;
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
i++;
break ;
}
}
int maxCount = 0;
HashSet< int > s = new HashSet< int >();
for (; i < n; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
maxCount = Math.Max(maxCount,
( int )s.Count);
s.Clear();
}
if (str[i] >= 'a' && str[i] <= 'z' )
s.Add(( int )str[i]);
}
return maxCount;
}
public static void Main(String []args)
{
String str = "zACaAbbaazzC" ;
Console.WriteLine(maxLower(str.ToCharArray()));
}
}
|
Javascript
<script>
function maxLower(str)
{
let n = str.length;
let i = 0;
for (; i < n; i++)
{
if (str[i].charCodeAt(0) >= 'A' .charCodeAt(0) && str[i].charCodeAt(0) <= 'Z' .charCodeAt(0))
{
i++;
break ;
}
}
let maxCount = 0;
let s = new Set();
for (; i < n; i++)
{
if (str[i].charCodeAt(0) >= 'A' .charCodeAt(0) && str[i].charCodeAt(0) <= 'Z' .charCodeAt(0))
{
maxCount = Math.max(maxCount, s.size);
s.clear();
}
if (str[i].charCodeAt(0) >= 'a' .charCodeAt(0) && str[i].charCodeAt(0) <= 'z' .charCodeAt(0))
s.add(str[i]);
}
return maxCount;
}
let str = "zACaAbbaazzC" ;
document.write(maxLower(str.split( "" )));
</script>
|
Time complexity : O(n)
Auxiliary Space: O(n).
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...