将所有小写字符转换为 ASCII 值与 k 互质的大写字符
给定一个整数“k”和一个由英文字符组成的字符串“str”。任务是将所有小写字符转换为 ASCII 值与 k 互质的大写字符。
例子:
Input: str = “geeksforgeeks”, k = 4
Output: GEEKSfOrGEEKS
‘f’ and ‘r’ are the only characters whose ASCII values aren’t co-prime with 4.
Input: str = “Ac”, k = 2
Output: AC
The only lower case character is ‘c’ and ASCII value of ‘c’ is 99 which is co-prime with 2.
方法:
- 遍历给定字符串中的所有字符以检查当前字符是否为小写,以及它的 ASCII 值是否与 'k' 互质
- 要检查互质,请检查带有 k 的值的 gcd 是否为“1”。
- 如果满足上述条件,则将该小写字母转换为大写字母。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// function to modify the string
void convert_str(string s, int k)
{
// length of the string
int l = s.length();
for (int i = 0; i < l; i++) {
int ascii = (int)s[i];
// check if the character is
// lowercase and co-prime with k
if (ascii >= 'a' && ascii <= 'z'
&& __gcd(ascii, k) == 1) {
// change the character
// to upper-case
char c = s[i] - 32;
s[i] = c;
}
}
cout << s << "\n";
}
// Driver code
int main()
{
string s = "geeksforgeeks";
int k = 4;
convert_str(s, k);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// function to modify the string
static void convert_str(String str, int k)
{
// length of the string
char[] s = str.toCharArray();
int l = s.length;
for (int i = 0; i < l; i++)
{
int ascii = (int) s[i];
// check if the character is
// lowercase and co-prime with k
if (ascii >= 'a' && ascii <= 'z'
&& __gcd(ascii, k) == 1)
{
// change the character
// to upper-case
char c = (char) (s[i] - 32);
s[i] = c;
}
}
System.out.println(String.valueOf(s));
}
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
{
return b;
}
if (b == 0)
{
return a;
}
// base case
if (a == b)
{
return a;
}
// a is greater
if (a > b)
{
return __gcd(a - b, b);
}
return __gcd(a, b - a);
}
// Driver code
public static void main(String[] args)
{
String s = "geeksforgeeks";
int k = 4;
convert_str(s, k);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
from math import gcd
# function to modify the string
def convert_str(s, k):
modified_string = ""
for i in range(0, len(s)):
ascii = ord(s[i])
# check if the character is
# lowercase and co-prime with k
if (ascii >= ord('a') and
ascii <= ord('z') and
gcd(ascii, k) == 1):
# change the character to upper-case
modified_string += chr(ascii - 32)
else:
modified_string += s[i]
print(modified_string)
# Driver code
if __name__ == "__main__":
s = "geeksforgeeks"
k = 4
convert_str(s, k)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// function to modify the string
static void convert_str(String str, int k)
{
// length of the string
char[] s = str.ToCharArray();
int l = s.Length;
for (int i = 0; i < l; i++)
{
int ascii = (int) s[i];
// check if the character is
// lowercase and co-prime with k
if (ascii >= 'a' && ascii <= 'z'
&& __gcd(ascii, k) == 1)
{
// change the character
// to upper-case
char c = (char) (s[i] - 32);
s[i] = c;
}
}
Console.WriteLine(String.Join("", s));
}
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
{
return b;
}
if (b == 0)
{
return a;
}
// base case
if (a == b)
{
return a;
}
// a is greater
if (a > b)
{
return __gcd(a - b, b);
}
return __gcd(a, b - a);
}
// Driver code
public static void Main()
{
String s = "geeksforgeeks";
int k = 4;
convert_str(s, k);
}
}
// This code is contributed by PrinciRaj1992
PHP
= ord('a') &&
$ascii <= ord('z') &&
__gcd($ascii, $k) == 1)
{
// change the character to upper-case
$modified_string = $modified_string.chr($ascii - 32);
}
else
{
$modified_string = $modified_string.$str[$i];
}
}
echo ($modified_string);
}
function __gcd($a, $b)
{
// Everything divides 0
if ($a == 0)
{
return $b;
}
if ($b == 0)
{
return $a;
}
// base case
if ($a == $b)
{
return $a;
}
// a is greater
if ($a > $b)
{
return __gcd($a - $b, $b);
}
return __gcd($a, $b - $a);
}
// Driver code
$s = "geeksforgeeks";
$k = 4;
convert_str($s, $k);
// This code is contributed by ita_c
?>
Javascript
输出:
GEEKSfOrGEEKS
时间复杂度: O(L),其中 L 是字符串的长度。
辅助空间: O(1)