Given string S of size N, the task is to find the single-digit sum by the repetitive sum of digits of the value obtained by the sum of order of all alphabets in the given string.
The order of alphabets is given by the position at which they occur in English Alaphabets.
Examples:
Input: S = “geek”
Output: 1
Explanation:
The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28.
The single digit sum obtained by sum of 28 = 2 + 8 = 10 = 1 + 0 = 1.
Input: S = “GeeksforGeeks”
Output: 7
Approach: The given problem can be solved by first find the sum of orders of all the alphabets present in the given string S by adding the value (S[i] – ‘a’ + 1) or (S[i] – ‘A’ + 1) if S[i] is lowercase or uppercase character. After finding the value of sum, find the single digit of this value using the approach discussed in this article.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findTheSum(string str)
{
string alpha;
for ( int i = 0; i < str.length(); i++) {
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
alpha.push_back(str[i]);
}
int score = 0, n = 0;
for ( int i = 0; i < alpha.length(); i++) {
if (alpha[i] >= 'A' && alpha[i] <= 'Z' )
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
return n;
}
int main()
{
string S = "GeeksforGeeks" ;
cout << findTheSum(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findTheSum( char []str)
{
String alpha= "" ;
for ( int i = 0 ; i < str.length; i++) {
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
alpha+=(str[i]);
}
int score = 0 , n = 0 ;
for ( int i = 0 ; i < alpha.length(); i++) {
if (alpha.charAt(i) >= 'A' && alpha.charAt(i) <= 'Z' )
score += alpha.charAt(i) - 'A' + 1 ;
else
score += alpha.charAt(i) - 'a' + 1 ;
}
while (score > 0 || n > 9 ) {
if (score == 0 ) {
score = n;
n = 0 ;
}
n += score % 10 ;
score /= 10 ;
}
return n;
}
public static void main(String[] args)
{
String S = "GeeksforGeeks" ;
System.out.print(findTheSum(S.toCharArray()));
}
}
|
Python3
def findTheSum( str ):
alpha = ""
for i in range ( 0 , len ( str )):
if (( str [i] > = 'A' and str [i] < = 'Z' ) or ( str [i] > = 'a' and str [i] < = 'z' )):
alpha + = str [i]
score = 0
n = 0
for i in range ( 0 , len (alpha)):
if (alpha[i] > = 'A' and alpha[i] < = 'Z' ):
score + = ord (alpha[i]) - ord ( 'A' ) + 1
else :
score + = ord (alpha[i]) - ord ( 'a' ) + 1
while (score > 0 or n > 9 ):
if (score = = 0 ):
score = n
n = 0
n + = score % 10
score = score / / 10
return n
if __name__ = = "__main__" :
S = "GeeksforGeeks"
print (findTheSum(S))
|
C#
using System;
class GFG {
static int findTheSum( string str)
{
string alpha = "" ;
for ( int i = 0; i < str.Length; i++) {
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
alpha += (str[i]);
}
int score = 0, n = 0;
for ( int i = 0; i < alpha.Length; i++) {
if (alpha[i] >= 'A' && alpha[i] <= 'Z' )
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
return n;
}
public static void Main()
{
string S = "GeeksforGeeks" ;
Console.WriteLine(findTheSum(S));
}
}
|
Javascript
<script>
function findTheSum(str) {
let alpha = [];
for (let i = 0; i < str.length; i++) {
if (
(str[i].charCodeAt(0) >= "A" .charCodeAt(0) &&
str[i].charCodeAt(0) <= "Z" .charCodeAt(0)) ||
(str[i].charCodeAt(0) >= "a" .charCodeAt(0) &&
str[i].charCodeAt(0) <= "z" .charCodeAt(0))
)
alpha.push(str[i]);
}
let score = 0,
n = 0;
for (let i = 0; i < alpha.length; i++) {
if (
alpha[i].charCodeAt(0) >= "A" .charCodeAt(0) &&
alpha[i].charCodeAt(0) <= "Z" .charCodeAt(0)
)
score += alpha[i].charCodeAt(0) - "A" .charCodeAt(0) + 1;
else score += alpha[i].charCodeAt(0) - "a" .charCodeAt(0) + 1;
}
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score = Math.floor(score / 10);
}
return n;
}
let S = "GeeksforGeeks" ;
document.write(findTheSum(S));
</script>
|
Time Complexity: O(N)
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!
Last Updated :
11 Nov, 2021
Like Article
Save Article