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++ program for the above approach #include <bits/stdc++.h> using namespace std;
int findTheSum(string str)
{ string alpha;
// Traverse the given string
for ( int i = 0; i < str.length(); i++) {
// If character is an alphabet
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
alpha.push_back(str[i]);
}
// Stores the sum of order of values
int score = 0, n = 0;
for ( int i = 0; i < alpha.length(); i++) {
// Find the score
if (alpha[i] >= 'A' && alpha[i] <= 'Z' )
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
// Find the single digit sum
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
// Return the resultant sum
return n;
} // Driver Code int main()
{ string S = "GeeksforGeeks" ;
cout << findTheSum(S);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG{
static int findTheSum( char []str)
{ String alpha= "" ;
// Traverse the given String
for ( int i = 0 ; i < str.length; i++) {
// If character is an alphabet
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
alpha+=(str[i]);
}
// Stores the sum of order of values
int score = 0 , n = 0 ;
for ( int i = 0 ; i < alpha.length(); i++) {
// Find the score
if (alpha.charAt(i) >= 'A' && alpha.charAt(i) <= 'Z' )
score += alpha.charAt(i) - 'A' + 1 ;
else
score += alpha.charAt(i) - 'a' + 1 ;
}
// Find the single digit sum
while (score > 0 || n > 9 ) {
if (score == 0 ) {
score = n;
n = 0 ;
}
n += score % 10 ;
score /= 10 ;
}
// Return the resultant sum
return n;
} // Driver Code public static void main(String[] args)
{ String S = "GeeksforGeeks" ;
System.out.print(findTheSum(S.toCharArray()));
} } // This code is contributed by 29AjayKumar |
# Python program for the above approach def findTheSum( str ):
alpha = ""
# Traverse the given string
for i in range ( 0 , len ( str )):
# If character is an alphabet
if (( str [i] > = 'A' and str [i] < = 'Z' ) or ( str [i] > = 'a' and str [i] < = 'z' )):
alpha + = str [i]
# Stores the sum of order of values
score = 0
n = 0
for i in range ( 0 , len (alpha)):
# Find the score
if (alpha[i] > = 'A' and alpha[i] < = 'Z' ):
score + = ord (alpha[i]) - ord ( 'A' ) + 1
else :
score + = ord (alpha[i]) - ord ( 'a' ) + 1
# Find the single digit sum
while (score > 0 or n > 9 ):
if (score = = 0 ):
score = n
n = 0
n + = score % 10
score = score / / 10
# Return the resultant sum
return n
# Driver Code if __name__ = = "__main__" :
S = "GeeksforGeeks"
print (findTheSum(S))
# This code is contributed by rakeshsahni
|
// C# program for the above approach using System;
class GFG {
static int findTheSum( string str)
{
string alpha = "" ;
// Traverse the given string
for ( int i = 0; i < str.Length; i++) {
// If character is an alphabet
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
alpha += (str[i]);
}
// Stores the sum of order of values
int score = 0, n = 0;
for ( int i = 0; i < alpha.Length; i++) {
// Find the score
if (alpha[i] >= 'A' && alpha[i] <= 'Z' )
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
// Find the single digit sum
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
// Return the resultant sum
return n;
}
// Driver Code
public static void Main()
{
string S = "GeeksforGeeks" ;
Console.WriteLine(findTheSum(S));
}
} // This code is contributed by ukasp. |
<script> // Javascript program for the above approach function findTheSum(str) {
let alpha = [];
// Traverse the given string
for (let i = 0; i < str.length; i++) {
// If character is an alphabet
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]);
}
// Stores the sum of order of values
let score = 0,
n = 0;
for (let i = 0; i < alpha.length; i++) {
// Find the score
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;
}
// Find the single digit sum
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score = Math.floor(score / 10);
}
// Return the resultant sum
return n;
} // Driver Code let S = "GeeksforGeeks" ;
document.write(findTheSum(S)); // This code is contributed by saurabh_jaiswal. </script> |
7
Time Complexity: O(N)
Auxiliary Space: O(1)