给定一个字符串str和一个字符ch,其中字符ch的按钮无法正常工作。按下该键后,它将切换大写锁定而不是该字母。该任务是打印在此类故障键盘上键入str后生成的字符串。
注意:该字符串可以同时包含大写和小写字符。
例子:
Input: str = “This keyboard is faulty.”, ch = ‘a’
Output: This keyboRD IS Fulty.
Explanation:
‘a’ is encountered at position 10 (0 based indexing), which turns Caps Lock ON, resulting in capitalizing the further characters till next ‘a’ is encountered at position 18, which turns it OFF.
Therefore the resultant string is “This keyboRD IS Fulty.”.
Input: str = “Read the phrase.”, ch = ‘z’
Output: Read the phrase.
Explanation: Since ‘z’ does not occur in the string, the string str is printed correctly.
方法:请按照以下步骤解决上述问题:
- 将布尔变量CapsLockOn初始化为false ,它将存储是否打开Caps Lock 。
- 通过遍历字符的给定字符串的字符并执行以下操作:
- 如果字符串中的任何字符与ch相同,则删除当前字符并反转CapsLockOn的值,并继续迭代字符串。
- 如果CapsLockOn为True ,则将当前字符更新为大写,反之亦然。
- 如果CapsLockOn为False ,则保持当前字符不变。
- 字符串完全遍历后,打印结果字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the updated string
// after typing on faulty keyboard
string faultyKeyboard(string str, char ch)
{
// Stores the status of Caps Lock
bool CapsLockOn = false;
int i = 0;
// Traverse over the string
while (i < str.length()) {
// If ch is lower case
if (str[i] == ch
|| str[i] == char(ch + 32)) {
// Toggle the caps lock
CapsLockOn = !CapsLockOn;
// Delete i-th character
str.erase(i, 1);
continue;
}
// If ch is upper case
else if (str[i] == ch
|| str[i] == char(ch + 32)) {
// Toggle the caps lock
CapsLockOn = !CapsLockOn;
// Delete i-th character
str.erase(i, 1);
continue;
}
// If CapsLockOn is true
else if (CapsLockOn) {
if (islower(str[i])) {
// Convert the lower case
// character to upper case
str[i] = toupper(str[i]);
}
else {
// Convert the upper case
// character to lower case
str[i] = tolower(str[i]);
}
}
// Increment for next iteration
i++;
}
// Return the resultant string
return str;
}
// Driver Code
int main()
{
// Given string str
string str = "This keyboard is faulty.";
char ch = 'a';
// Function Call
cout << faultyKeyboard(str, ch);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to return the updated string
// after typing on faulty keyboard
static String faultyKeyboard(String str, char ch)
{
// Stores the status of Caps Lock
boolean CapsLockOn = false;
int i = 0;
// Traverse over the string
while (i < str.length())
{
// If ch is lower case
if (str.charAt(i) == ch ||
str.charAt(i) == (char)((int)ch + 32))
{
// Toggle the caps lock
CapsLockOn = !CapsLockOn;
// Delete i-th character
str = str.substring(0, i) +
str.substring(i + 1);
// str.replace(str.charAt(i),'');
continue;
}
// If ch is upper case
else if (str.charAt(i) == ch ||
str.charAt(i) == (char)((int)ch + 32))
{
// Toggle the caps lock
CapsLockOn = !CapsLockOn;
// Delete i-th character
str = str.substring(0, i) +
str.substring(i + 1);
// str.replace(str.charAt(i),'');
continue;
}
// If CapsLockOn is true
else if (CapsLockOn)
{
if (str.charAt(i) >='a' &&
str.charAt(i) <= 'z')
{
// Convert the lower case
// character to upper case
char c = Character.toUpperCase(
str.charAt(i));
str = str.substring(0, i) + c +
str.substring(i + 1);
// str = str.replace(str.charAt(i),
// Character.toUpperCase(str.charAt(i)));
}
else
{
// Convert the upper case
// character to lower case
char c = Character.toLowerCase(
str.charAt(i));
str = str.substring(0, i) + c +
str.substring(i + 1);
// str = str.replace(str.charAt(i),
// Character.toLowerCase(str.charAt(i)));
}
}
// Increment for next iteration
i++;
}
// Return the resultant string
return str;
}
// Driver Code
public static void main(String args[])
{
// Given string str
String str = "This keyboard is faulty.";
char ch = 'a';
// Function Call
System.out.print(faultyKeyboard(str, ch));
}
}
// This code is contributed by ipg2016107
Python3
# Python 3 program for the above approach
# Function to return the updated string
# after typing on faulty keyboard
def faultyKeyboard(str, ch):
# Stores the status of Caps Lock
CapsLockOn = False
i = 0
# Traverse over the string
while (i < len(str)):
# If ch is lower case
if (str[i] == ch or str[i] == chr(ord(ch) + 32)):
# Toggle the caps lock
if(CapsLockOn == False):
CapsLockOn = True
else:
CapsLockOn = False
# Delete i-th character
str = str.replace(str[i],'', 1)
continue
# If ch is upper case
elif(str[i] == ch or str[i] == chr(ord(ch) + 32)):
# Toggle the caps lock
if(CapsLockOn==False):
CapsLockOn = True
else:
CapsLockOn = False
# Delete i-th character
str = str.replace(str[i],'', 1)
continue
# If CapsLockOn is true
elif (CapsLockOn):
if (ord(str[i]) >= 97 and ord(str[i]) <= 122):
# Convert the lower case
# character to upper case
#indexes = set((i))
chars = list(str)
chars[i] = chars[i].upper()
str = ''.join(chars)
else:
#indexes = set((i))
chars = list(str)
chars[i] = chars[i].lower()
str = ''.join(chars)
# Increment for next iteration
i += 1
# Return the resultant string
return str
# Driver Code
if __name__ == '__main__':
# Given string str
str = "This keyboard is faulty."
ch = 'a'
# Function Call
print(faultyKeyboard(str, ch))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to return the updated
// string after typing on faulty
// keyboard
static string faultyKeyboard(string str,
char ch)
{
// Stores the status of
// Caps Lock
bool CapsLockOn = false;
int i = 0;
// Traverse over the string
while (i < str.Length)
{
// If ch is lower case
if (str[i] == ch ||
str[i] == (char)(ch + 32))
{
// Toggle the caps lock
CapsLockOn = !CapsLockOn;
// Delete i-th character
str = str.Substring(0, i) +
str.Substring(i + 1);
continue;
}
// If ch is upper case
else if (str[i] == ch ||
str[i] == (char)(ch + 32))
{
// Toggle the caps lock
CapsLockOn = !CapsLockOn;
// Delete i-th character
str = str.Substring(0, i) +
str.Substring(i + 1);
continue;
}
// If CapsLockOn is true
else if (CapsLockOn)
{
if (str[i] >= 'a' &&
str[i] <= 'z')
{
// Convert the lower case
// character to upper case
char c = (char)(str[i] - 32);
str = str.Substring(0, i) + c +
str.Substring(i + 1);
}
else if (str[i] >= 'A' &&
str[i] <= 'Z')
{
// Convert the upper case
// character to lower case
char c = (char)(str[i] + 32);
str = str.Substring(0, i) + c +
str.Substring(i + 1);
}
}
// Increment for next
// iteration
i++;
}
// Return the resultant
// string
return str;
}
// Driver Code
public static void Main()
{
// Given string str
string str = "This keyboard is faulty.";
char ch = 'a';
// Function Call
Console.Write(faultyKeyboard(str, ch));
}
}
// This code is contributed by Chitranayal
This keyboRD IS Fulty.
时间复杂度: O(N)
辅助空间: O(1)