Find the character made by adding all the characters of the given string
Last Updated :
11 Sep, 2023
Given a string str consisting of lowercase English alphabets. The task is to add all the character values i.e. ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, …, ‘z’ = 26 and output the character corresponding to the sum value. If it exceeds 26 then take sum % 26.
Examples:
Input: str = “gfg”
Output: t
(g + f + g) = 7 + 6 + 7 = 20 and t = 20
Input: str = “geeks”
Output: u
Approach:
- Initialize a variable sum to 0.
- Loop through each character of the string.
- For each character, get its ASCII value by typecasting it to an integer and subtracting 96 from it (as ‘a’ has an ASCII value of 97, and we want ‘a’ to have a value of 1).
- Add this value to the sum.
- If sum is greater than 26, take its modulo with 26.
- Convert the value obtained in step 5 to the corresponding character value by adding 96 to it and typecasting it to a character. If the value is 0, set it to ‘z’.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char getChar(string str)
{
int n = str.length();
int max_sum = 0;
char max_char = 'a' ;
for ( int i = 0; i < n; i++) {
int sum = 0;
for ( int j = i; j < n; j++) {
sum += (str[j] - 'a' + 1);
if (sum > max_sum) {
max_sum = sum;
if (max_sum % 26 == 0)
max_char = 'z' ;
else
max_char = ( char )( 'a' + max_sum % 26 - 1);
}
}
}
return max_char;
}
int main()
{
string str = "gfg" ;
cout << getChar(str);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
String str = "gfg" ;
System.out.println(getChar(str));
}
public static char getChar(String str) {
int n = str.length();
int maxSum = 0 ;
char maxChar = 'a' ;
for ( int i = 0 ; i < n; i++) {
int sum = 0 ;
for ( int j = i; j < n; j++) {
sum += (str.charAt(j) - 'a' + 1 );
if (sum > maxSum) {
maxSum = sum;
if (maxSum % 26 == 0 )
maxChar = 'z' ;
else
maxChar = ( char ) ( 'a' + maxSum % 26 - 1 );
}
}
}
return maxChar;
}
}
|
Python
def get_char(s):
n = len (s)
max_sum = 0
max_char = 'a'
for i in range (n):
sum_val = 0
for j in range (i, n):
sum_val + = ( ord (s[j]) - ord ( 'a' ) + 1 )
if sum_val > max_sum:
max_sum = sum_val
if max_sum % 26 = = 0 :
max_char = 'z'
else :
max_char = chr ( ord ( 'a' ) + max_sum % 26 - 1 )
return max_char
def main():
s = "gfg"
print (get_char(s))
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program {
static char GetChar( string str)
{
int n = str.Length;
int maxSum = 0;
char maxChar = 'a' ;
for ( int i = 0; i < n; i++) {
int sum = 0;
for ( int j = i; j < n; j++) {
sum += (str[j] - 'a' + 1);
if (sum > maxSum) {
maxSum = sum;
if (maxSum % 26 == 0)
maxChar = 'z' ;
else
maxChar
= ( char )( 'a' + maxSum % 26 - 1);
}
}
}
return maxChar;
}
static void Main()
{
string str = "gfg" ;
Console.WriteLine(GetChar(str));
}
}
|
Javascript
function getChar(str) {
let n = str.length;
let maxSum = 0;
let maxChar = 'a' ;
for (let i = 0; i < n; i++) {
let sum = 0;
for (let j = i; j < n; j++) {
sum += (str[j].charCodeAt(0) - 'a' .charCodeAt(0) + 1);
if (sum > maxSum) {
maxSum = sum;
if (maxSum % 26 === 0)
maxChar = 'z' ;
else
maxChar = String.fromCharCode( 'a' .charCodeAt(0) + (maxSum % 26) - 1);
}
}
}
return maxChar;
}
let str = "gfg" ;
console.log(getChar(str));
|
Time Complexity: O(n^2) where n is the length of the given string.
Space Complexity: O(1), as we are not using any extra space.
Approach:
- Find the sum of all characters of the string and store it in a variable sum.
- If sum % 26 = 0 then print ‘z’.
- Else update sum = sum % 26 and print (sum + ‘a’ + 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char getChar(string str)
{
int sum = 0;
for ( int i = 0; i < str.length(); i++) {
sum += (str[i] - 'a' + 1);
}
if (sum % 26 == 0)
return 'z' ;
else {
sum = sum % 26;
return ( char )( 'a' + sum - 1);
}
}
int main()
{
string str = "gfg" ;
cout << getChar(str);
return 0;
}
|
Java
class GFG
{
static char getChar(String str)
{
int sum = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
sum += (str.charAt(i) - 'a' + 1 );
}
if (sum % 26 == 0 )
return 'z' ;
else
{
sum = sum % 26 ;
return ( char )( 'a' + sum - 1 );
}
}
public static void main (String[] args)
{
String str = "gfg" ;
System.out.println(getChar(str));
}
}
|
Python3
def getChar(strr):
summ = 0
for i in range ( len (strr)):
summ + = ( ord (strr[i]) - ord ( 'a' ) + 1 )
if (summ % 26 = = 0 ):
return ord ( 'z' )
else :
summ = summ % 26
return chr ( ord ( 'a' ) + summ - 1 )
strr = "gfg"
print (getChar(strr))
|
C#
using System;
class GFG
{
static char getChar(String str)
{
int sum = 0;
for ( int i = 0; i < str.Length; i++)
{
sum += (str[i] - 'a' + 1);
}
if (sum % 26 == 0)
return 'z' ;
else
{
sum = sum % 26;
return ( char )( 'a' + sum - 1);
}
}
public static void Main (String[] args)
{
String str = "gfg" ;
Console.WriteLine(getChar(str));
}
}
|
Javascript
<script>
function getChar(str)
{
let sum = 0;
for (let i = 0; i < str.length; i++)
{
sum += (str[i].charCodeAt() - 'a' .charCodeAt() + 1);
}
if (sum % 26 == 0)
return 'z' ;
else {
sum = sum % 26;
return String.fromCharCode( 'a' .charCodeAt() + sum - 1);
}
}
let str = "gfg" ;
document.write(getChar(str));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...