Given a string S consisting of digits [0 – 9] and lowercase alphabets, the task is to calculate the sum of all numbers represented by continuous sequences of digits present in the string S.
Examples:
Input: S = “11aa32bbb5”
Output: 48
Explanation:
The consecutive sequence of numbers present in the string S are {11, 32, 5}.
Therefore, sum = 11 + 32 + 5 = 48Input: s = “5an63ff2”
Output: 70
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say curr, to store the current sequence of consecutive digits
- Iterate over the characters of the string.
- If the current character is not a digit, add the current value of curr to the final answer. Reset curr to 0.
- Otherwise, append the current digit to curr.
- Finally, return the final answer.
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std;
// Function to calculate the sum of // numbers formed by consecutive // sequences of digits present in the string int sumOfDigits(string s)
{ // Stores consecutive digits
// present in the string
int curr = 0;
// Stores the sum
int ret = 0;
// Iterate over characters
// of the input string
for ( auto & ch : s) {
// If current character is a digit
if ( isdigit (ch)) {
// Append current digit to curr
curr = curr * 10 + ch - '0' ;
}
else {
// Add curr to sum
ret += curr;
// Reset curr
curr = 0;
}
}
ret += curr;
return ret;
} // Driver Code int main()
{ string S = "11aa32bbb5" ;
cout << sumOfDigits(S);
return 0;
} |
Java
// Java program to implement // the above approach import java.io.*;
import java.util.*;
class GFG
{ // Function to calculate the sum of
// numbers formed by consecutive
// sequences of digits present in the string
static int sumOfDigits(String s)
{
// Stores consecutive digits
// present in the string
int curr = 0 ;
// Stores the sum
int ret = 0 ;
// Iterate over characters
// of the input string
for ( char ch : s.toCharArray())
{
// If current character is a digit
if (ch >= 48 && ch <= 57 )
{
// Append current digit to curr
curr = curr * 10 + ch - '0' ;
}
else
{
// Add curr to sum
ret += curr;
// Reset curr
curr = 0 ;
}
}
ret += curr;
return ret;
}
// Driver Code
public static void main(String[] args)
{
String S = "11aa32bbb5" ;
System.out.print(sumOfDigits(S));
}
} // This code is contributed by splevel62. |
Python3
# Python3 program for the above approach # Function to calculate the sum of # numbers formed by consecutive # sequences of digits present in the string def sumOfDigits(s) :
# Stores consecutive digits
# present in the string
curr = 0
# Stores the sum
ret = 0
# Iterate over characters
# of the input string
for ch in s :
# If current character is a digit
if ( ord (ch) > = 48 and ord (ch) < = 57 ) :
# Append current digit to curr
curr = curr * 10 + ord (ch) - ord ( '0' )
else :
# Add curr to sum
ret + = curr
# Reset curr
curr = 0
ret + = curr
return ret
# Driver Code S = "11aa32bbb5"
print (sumOfDigits(S))
# This code is contributed by code_hunt. |
C#
// C# Program to implement // the above approach using System;
class GFG
{ // Function to calculate the sum of
// numbers formed by consecutive
// sequences of digits present in the string
static int sumOfDigits( string s)
{
// Stores consecutive digits
// present in the string
int curr = 0;
// Stores the sum
int ret = 0;
// Iterate over characters
// of the input string
foreach ( char ch in s)
{
// If current character is a digit
if (ch >= 48 && ch <= 57)
{
// Append current digit to curr
curr = curr * 10 + ch - '0' ;
}
else
{
// Add curr to sum
ret += curr;
// Reset curr
curr = 0;
}
}
ret += curr;
return ret;
}
// Driver code
static void Main() {
string S = "11aa32bbb5" ;
Console.WriteLine(sumOfDigits(S));
}
} // This code is contributed by divyeshrabadiya07. |
Javascript
<script> // Javascript Program to implement // the above approach // Function to calculate the sum of // numbers formed by consecutive // sequences of digits present in the string function sumOfDigits(s)
{ // Stores consecutive digits
// present in the string
var curr = 0;
// Stores the sum
var ret = 0;
// Iterate over characters
// of the input string
s.split( '' ).forEach(ch => {
// If current character is a digit
if (parseInt(ch)) {
// Append current digit to curr
curr = curr * 10 + ch.charCodeAt(0) - '0' .charCodeAt(0);
}
else {
// Add curr to sum
ret += curr;
// Reset curr
curr = 0;
}
});
ret += curr;
return ret;
} // Driver Code var S = "11aa32bbb5" ;
document.write( sumOfDigits(S)); // This code is contributed by importantly. </script> |
Output:
48
Time Complexity: O(N)
Auxiliary Space: O(1)