给定一个字符串str ,其中包含大写和小写字符。在单个操作中,任何小写字符都可以转换为大写字符,反之亦然。任务是打印最少数量的此类操作,以使结果字符串由零个或多个大写字符,然后是零个或多个小写字符。
例子:
Input: str = “geEks”
Output: 1
Either the first 2 characters can be converted to uppercase characters i.e. “GEEks” with 2 operations.
Or the third character can be converted to a lowercase character i.e. “geeks” with a single operation.
Input: str = “geek”
Output: 0
The string is already in the specified format.
方法:有两种可能的情况:
- 查找字符串最后一个大写字符的索引,并将出现在其前面的所有小写字符转换为大写字符。
- 或者,找到字符串第一个小写字符的索引,然后将其后出现的所有大写字符转换为小写字符。
选择所需操作最少的情况。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum
// number of operations required
int minOperations(string str, int n)
{
// To store the indices of the last uppercase
// and the first lowercase character
int i, lastUpper = -1, firstLower = -1;
// Find the last uppercase character
for (i = n - 1; i >= 0; i--)
{
if (isupper(str[i]))
{
lastUpper = i;
break;
}
}
// Find the first lowercase character
for (i = 0; i < n; i++)
{
if (islower(str[i]))
{
firstLower = i;
break;
}
}
// If all the characters are either
// uppercase or lowercase
if (lastUpper == -1 || firstLower == -1)
return 0;
// Count of uppercase characters that appear
// after the first lowercase character
int countUpper = 0;
for (i = firstLower; i < n; i++)
{
if (isupper(str[i]))
{
countUpper++;
}
}
// Count of lowercase characters that appear
// before the last uppercase character
int countLower = 0;
for (i = 0; i < lastUpper; i++)
{
if (islower(str[i]))
{
countLower++;
}
}
// Return the minimum operations required
return min(countLower, countUpper);
}
// Driver Code
int main()
{
string str = "geEksFOrGEekS";
int n = str.length();
cout << minOperations(str, n) << endl;
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum
// number of operations required
static int minOperations(String str, int n)
{
// To store the indices of the last uppercase
// and the first lowercase character
int i, lastUpper = -1, firstLower = -1;
// Find the last uppercase character
for (i = n - 1; i >= 0; i--) {
if (Character.isUpperCase(str.charAt(i))) {
lastUpper = i;
break;
}
}
// Find the first lowercase character
for (i = 0; i < n; i++) {
if (Character.isLowerCase(str.charAt(i))) {
firstLower = i;
break;
}
}
// If all the characters are either
// uppercase or lowercase
if (lastUpper == -1 || firstLower == -1)
return 0;
// Count of uppercase characters that appear
// after the first lowercase character
int countUpper = 0;
for (i = firstLower; i < n; i++) {
if (Character.isUpperCase(str.charAt(i))) {
countUpper++;
}
}
// Count of lowercase characters that appear
// before the last uppercase character
int countLower = 0;
for (i = 0; i < lastUpper; i++) {
if (Character.isLowerCase(str.charAt(i))) {
countLower++;
}
}
// Return the minimum operations required
return Math.min(countLower, countUpper);
}
// Driver Code
public static void main(String args[])
{
String str = "geEksFOrGEekS";
int n = str.length();
System.out.println(minOperations(str, n));
}
}
Python 3
# Python 3 implementation of the approach
# Function to return the minimum
# number of operations required
def minOperations(str, n):
# To store the indices of the last uppercase
# and the first lowercase character
lastUpper = -1
firstLower = -1
# Find the last uppercase character
for i in range( n - 1, -1, -1):
if (str[i].isupper()):
lastUpper = i
break
# Find the first lowercase character
for i in range(n):
if (str[i].islower()):
firstLower = i
break
# If all the characters are either
# uppercase or lowercase
if (lastUpper == -1 or firstLower == -1):
return 0
# Count of uppercase characters that appear
# after the first lowercase character
countUpper = 0
for i in range( firstLower,n):
if (str[i].isupper()):
countUpper += 1
# Count of lowercase characters that appear
# before the last uppercase character
countLower = 0
for i in range(lastUpper):
if (str[i].islower()):
countLower += 1
# Return the minimum operations required
return min(countLower, countUpper)
# Driver Code
if __name__ == "__main__":
str = "geEksFOrGEekS"
n = len(str)
print(minOperations(str, n))
# This code is contributed by Ita_c.
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum
// number of operations required
static int minOperations(string str, int n)
{
// To store the indices of the last uppercase
// and the first lowercase character
int i, lastUpper = -1, firstLower = -1;
// Find the last uppercase character
for (i = n - 1; i >= 0; i--)
{
if (Char.IsUpper(str[i]))
{
lastUpper = i;
break;
}
}
// Find the first lowercase character
for (i = 0; i < n; i++)
{
if (Char.IsLower(str[i]))
{
firstLower = i;
break;
}
}
// If all the characters are either
// uppercase or lowercase
if (lastUpper == -1 || firstLower == -1)
return 0;
// Count of uppercase characters that appear
// after the first lowercase character
int countUpper = 0;
for (i = firstLower; i < n; i++)
{
if (Char.IsUpper(str[i]))
{
countUpper++;
}
}
// Count of lowercase characters that appear
// before the last uppercase character
int countLower = 0;
for (i = 0; i < lastUpper; i++)
{
if (Char.IsLower(str[i]))
{
countLower++;
}
}
// Return the minimum operations required
return Math.Min(countLower, countUpper);
}
// Driver Code
public static void Main()
{
string str = "geEksFOrGEekS";
int n = str.Length;
Console.WriteLine(minOperations(str, n));
}
}
// This code is contributed by Ryuga
PHP
= 0; $i--)
{
if (ctype_upper($str[$i]))
{
$lastUpper = $i;
break;
}
}
// Find the first lowercase character
for ($i = 0; $i < $n; $i++)
{
if (ctype_lower($str[$i]))
{
$firstLower = $i;
break;
}
}
// If all the characters are either
// uppercase or lowercase
if ($lastUpper == -1 || $firstLower == -1)
return 0;
// Count of uppercase characters that appear
// after the first lowercase character
$countUpper = 0;
for ($i = $firstLower; $i < $n; $i++)
{
if (ctype_upper($str[$i]))
{
$countUpper++;
}
}
// Count of lowercase characters that appear
// before the last uppercase character
$countLower = 0;
for ($i = 0; $i < $lastUpper; $i++)
{
if (ctype_lower($str[$i]))
{
$countLower++;
}
}
// Return the minimum operations required
return min($countLower, $countUpper);
}
// Driver Code
{
$str = "geEksFOrGEekS";
$n = strlen($str);
echo(minOperations($str, $n));
}
// This code is contributed by Code_Mech
?>
输出:
6
时间复杂度: O(N),其中N是字符串的长度。