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 = 20Input: str = “neveropen”
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; // Function to return the required character 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; } // Driver code 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
# Function to return the required character 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 # Driver code def main(): s = "gfg" print (get_char(s)) if __name__ = = "__main__" : main() |
C#
using System; class Program { // Function to return the required character 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; } // Driver code static void Main() { string str = "gfg" ; Console.WriteLine(GetChar(str)); } } // sinudp5vi |
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; } // Test the getChar function let str = "gfg" ; console.log(getChar(str)); |
t
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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the required character char getChar(string str) { // To store the sum of the characters // of the given string int sum = 0; for ( int i = 0; i < str.length(); i++) { // Add the current character to the sum sum += (str[i] - 'a' + 1); } // Return the required character if (sum % 26 == 0) return 'z' ; else { sum = sum % 26; return ( char )( 'a' + sum - 1); } } // Driver code int main() { string str = "gfg" ; cout << getChar(str); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the required character static char getChar(String str) { // To store the sum of the characters // of the given string int sum = 0 ; for ( int i = 0 ; i < str.length(); i++) { // Add the current character to the sum sum += (str.charAt(i) - 'a' + 1 ); } // Return the required character if (sum % 26 == 0 ) return 'z' ; else { sum = sum % 26 ; return ( char )( 'a' + sum - 1 ); } } // Driver code public static void main (String[] args) { String str = "gfg" ; System.out.println(getChar(str)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the required character def getChar(strr): # To store the summ of the characters # of the given string summ = 0 for i in range ( len (strr)): # Add the current character to the summ summ + = ( ord (strr[i]) - ord ( 'a' ) + 1 ) # Return the required character if (summ % 26 = = 0 ): return ord ( 'z' ) else : summ = summ % 26 return chr ( ord ( 'a' ) + summ - 1 ) # Driver code strr = "gfg" print (getChar(strr)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the required character static char getChar(String str) { // To store the sum of the characters // of the given string int sum = 0; for ( int i = 0; i < str.Length; i++) { // Add the current character to the sum sum += (str[i] - 'a' + 1); } // Return the required character if (sum % 26 == 0) return 'z' ; else { sum = sum % 26; return ( char )( 'a' + sum - 1); } } // Driver code public static void Main (String[] args) { String str = "gfg" ; Console.WriteLine(getChar(str)); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript implementation of the approach // Function to return the required character function getChar(str) { // To store the sum of the characters // of the given string let sum = 0; for (let i = 0; i < str.length; i++) { // Add the current character to the sum sum += (str[i].charCodeAt() - 'a' .charCodeAt() + 1); } // Return the required character if (sum % 26 == 0) return 'z' ; else { sum = sum % 26; return String.fromCharCode( 'a' .charCodeAt() + sum - 1); } } let str = "gfg" ; document.write(getChar(str)); // This code is contributed by divyeshrabadiya07. </script> |
t
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!