Given an integer ‘k’ and a string ‘str’ consisting of characters from English alphabets. The task is to convert all lower case character to uppercase whose ASCII value is co-prime with k.
Examples:
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.
Approach:
- Iterate over all characters in the given string to check whether the current character is lowercase and if it’s ASCII value is co-prime with ‘k’
- To check for co-prime, check that if the gcd of the value with k is ‘1’ or not.
- If the above condition is satisfied then convert that lowercase alphabet to uppercase alphabet.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void convert_str(string s, int k)
{
int l = s.length();
for ( int i = 0; i < l; i++) {
int ascii = ( int )s[i];
if (ascii >= 'a' && ascii <= 'z'
&& __gcd(ascii, k) == 1) {
char c = s[i] - 32;
s[i] = c;
}
}
cout << s << "\n" ;
}
int main()
{
string s = "geeksforgeeks" ;
int k = 4;
convert_str(s, k);
return 0;
}
|
Java
class GFG
{
static void convert_str(String str, int k)
{
char [] s = str.toCharArray();
int l = s.length;
for ( int i = 0 ; i < l; i++)
{
int ascii = ( int ) s[i];
if (ascii >= 'a' && ascii <= 'z'
&& __gcd(ascii, k) == 1 )
{
char c = ( char ) (s[i] - 32 );
s[i] = c;
}
}
System.out.println(String.valueOf(s));
}
static int __gcd( int a, int b)
{
if (a == 0 )
{
return b;
}
if (b == 0 )
{
return a;
}
if (a == b)
{
return a;
}
if (a > b)
{
return __gcd(a - b, b);
}
return __gcd(a, b - a);
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
int k = 4 ;
convert_str(s, k);
}
}
|
Python3
from math import gcd
def convert_str(s, k):
modified_string = ""
for i in range ( 0 , len (s)):
ascii = ord (s[i])
if (ascii > = ord ( 'a' ) and
ascii < = ord ( 'z' ) and
gcd(ascii, k) = = 1 ):
modified_string + = chr (ascii - 32 )
else :
modified_string + = s[i]
print (modified_string)
if __name__ = = "__main__" :
s = "geeksforgeeks"
k = 4
convert_str(s, k)
|
C#
using System;
class GFG
{
static void convert_str(String str, int k)
{
char [] s = str.ToCharArray();
int l = s.Length;
for ( int i = 0; i < l; i++)
{
int ascii = ( int ) s[i];
if (ascii >= 'a' && ascii <= 'z'
&& __gcd(ascii, k) == 1)
{
char c = ( char ) (s[i] - 32);
s[i] = c;
}
}
Console.WriteLine(String.Join( "" , s));
}
static int __gcd( int a, int b)
{
if (a == 0)
{
return b;
}
if (b == 0)
{
return a;
}
if (a == b)
{
return a;
}
if (a > b)
{
return __gcd(a - b, b);
}
return __gcd(a, b - a);
}
public static void Main()
{
String s = "geeksforgeeks" ;
int k = 4;
convert_str(s, k);
}
}
|
PHP
<?php
function convert_str( $str , $k )
{
$l = strlen ( $str );
$modified_string = "" ;
for ( $i = 0; $i < $l ; $i ++)
{
$ascii = ord( $str [ $i ]);
if ( $ascii >= ord( 'a' ) &&
$ascii <= ord( 'z' ) &&
__gcd( $ascii , $k ) == 1)
{
$modified_string = $modified_string . chr ( $ascii - 32);
}
else
{
$modified_string = $modified_string . $str [ $i ];
}
}
echo ( $modified_string );
}
function __gcd( $a , $b )
{
if ( $a == 0)
{
return $b ;
}
if ( $b == 0)
{
return $a ;
}
if ( $a == $b )
{
return $a ;
}
if ( $a > $b )
{
return __gcd( $a - $b , $b );
}
return __gcd( $a , $b - $a );
}
$s = "geeksforgeeks" ;
$k = 4;
convert_str( $s , $k );
?>
|
Javascript
<script>
function convert_str(s, k) {
var l = s.length;
var newStr = "" ;
for ( var i = 0; i < l; i++) {
var ascii = s[i].charCodeAt(0);
if (
ascii >= "a" .charCodeAt(0) &&
ascii <= "z" .charCodeAt(0) &&
__gcd(ascii, k) === 1
) {
var c = String.fromCharCode(s[i].charCodeAt(0) - 32);
newStr += c;
} else {
newStr += s[i];
}
}
document.write(newStr);
}
function __gcd(a, b) {
if (a === 0) {
return b;
}
if (b === 0) {
return a;
}
if (a === b) {
return a;
}
if (a > b) {
return __gcd(a - b, b);
}
return __gcd(a, b - a);
}
var s = "geeksforgeeks" ;
var k = 4;
convert_str(s, k);
</script>
|
Complexity Analysis:
- Time Complexity: O(L), where L is the length of string.
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!