给定一个包含小写和大写字母的字符串,找出两个大写字母之间存在的不同小写字母的最大计数。
例子:
Input : zACaAbbaazzC
Output : The maximum count = 3
Input : edxedxxxCQiIVmYEUtLi
Output : The maximum count = 1
方法一(使用字符计数数组):
- 声明一个大小为 26 的数组,其中数组的每个索引代表英文字母表中的一个字符
- 在整个长度上迭代字符串
- 对于每个小写字符,将相应数组的索引增加 1。
- 对于每个大写字符,迭代数组并计算值大于零的位置数。
- 如果此计数大于最大计数,则更新最大计数器,并将数组初始化为 0 。
下面是上述方法的实现。
C++
// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include
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;
}
Java
// 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
Python3
# 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
C#
// C# Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
using System;
using System.Collections.Generic;
class GFG
{
static 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[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 = new int[MAX_CHAR];
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 = Math.Max(maxCount, currCount);
// Reset count array
Array.Fill(count, 0);
}
// If character is in lowercase
if (str[i] >= 'a' && str[i] <= 'z')
{
count[str[i] - 'a']++;
}
}
return maxCount;
}
// Driver code
public static void Main(String[] args)
{
String str = "zACaAbbaazzC";
Console.WriteLine(maxLower(str));
}
}
// This code is contributed by PrinciRaj1992
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
?>
Javascript
C++
// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include
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 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;
}
Java
// Java Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
import java.util.*;
class GFG
{
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
static int maxLower(char[] 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;
HashSet s = new HashSet();
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 = Math.max(maxCount, (int)s.size());
// clear the set
s.clear();
}
// If character is in lowercase
if (str[i] >= 'a' && str[i] <= 'z')
s.add((int)str[i]);
}
return maxCount;
}
// Driver Code
public static void main(String args[])
{
String str = "zACaAbbaazzC";
System.out.println(maxLower(str.toCharArray()));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 Program to find maximum
# lowercase alphabets present
# between two uppercase alphabets
# 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 = 3;
s = set()
for i in range(n):
# If character is in
# uppercase,
if (str[i] >= 'A' and
str[i] <= 'Z'):
# Update maximum count if
# lowercase character before
# this is more.
maxCount = max(maxCount,
len(s));
# clear the set
s.clear();
# If character is in
# lowercase
if (str[i] >= 'a' and
str[i] <= 'z'):
s.add(str[i]);
return maxCount;
# Driver Code
if __name__ == '__main__':
str = "zACaAbbaazzC";
print(maxLower(str));
# This code is contributed by 29AjayKumar
C#
// C# Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
using System;
using System.Collections.Generic;
class GFG
{
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
static int maxLower(char[] 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;
HashSet s = new HashSet();
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 = Math.Max(maxCount,
(int)s.Count);
// clear the set
s.Clear();
}
// If character is in lowercase
if (str[i] >= 'a' && str[i] <= 'z')
s.Add((int)str[i]);
}
return maxCount;
}
// Driver Code
public static void Main(String []args)
{
String str = "zACaAbbaazzC";
Console.WriteLine(maxLower(str.ToCharArray()));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
3
时间复杂度: O(n)。方法二(使用哈希表):在该方法中,我们广泛使用了C++ STL容器unordered_set。
下面是上述方法的实现:
C++
// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include
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 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;
}
Java
// Java Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
import java.util.*;
class GFG
{
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
static int maxLower(char[] 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;
HashSet s = new HashSet();
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 = Math.max(maxCount, (int)s.size());
// clear the set
s.clear();
}
// If character is in lowercase
if (str[i] >= 'a' && str[i] <= 'z')
s.add((int)str[i]);
}
return maxCount;
}
// Driver Code
public static void main(String args[])
{
String str = "zACaAbbaazzC";
System.out.println(maxLower(str.toCharArray()));
}
}
// This code is contributed by PrinciRaj1992
蟒蛇3
# Python3 Program to find maximum
# lowercase alphabets present
# between two uppercase alphabets
# 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 = 3;
s = set()
for i in range(n):
# If character is in
# uppercase,
if (str[i] >= 'A' and
str[i] <= 'Z'):
# Update maximum count if
# lowercase character before
# this is more.
maxCount = max(maxCount,
len(s));
# clear the set
s.clear();
# If character is in
# lowercase
if (str[i] >= 'a' and
str[i] <= 'z'):
s.add(str[i]);
return maxCount;
# Driver Code
if __name__ == '__main__':
str = "zACaAbbaazzC";
print(maxLower(str));
# This code is contributed by 29AjayKumar
C#
// C# Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
using System;
using System.Collections.Generic;
class GFG
{
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
static int maxLower(char[] 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;
HashSet s = new HashSet();
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 = Math.Max(maxCount,
(int)s.Count);
// clear the set
s.Clear();
}
// If character is in lowercase
if (str[i] >= 'a' && str[i] <= 'z')
s.Add((int)str[i]);
}
return maxCount;
}
// Driver Code
public static void Main(String []args)
{
String str = "zACaAbbaazzC";
Console.WriteLine(maxLower(str.ToCharArray()));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
3
时间复杂度: 0(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。