Number of even substrings in a string of digits
Last Updated :
26 Oct, 2023
Given a string of digits 0 – 9. The task is to count a number of substrings which when converting into integer form an even number.
Examples :
Input : str = "1234".
Output : 6
"2", "4", "12", "34", "234", "1234"
are 6 substring which are even.
Input : str = "154".
Output : 3
Input : str = "15".
Output : 0
For a number to be even, the substring must end with an even digit. We find all the even digits in the string and for each even digit, count the number of substrings ending with it. Now, observe that the number of substrings will be an index of that even digit plus one.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int evenNumSubstring( char str[])
{
int len = strlen (str);
int count = 0;
for ( int i = 0; i < len; i++)
{
int temp = str[i] - '0' ;
if (temp % 2 == 0)
count += (i + 1);
}
return count;
}
int main()
{
char str[] = "1234" ;
cout << evenNumSubstring(str) << endl;
return 0;
}
|
Java
public class GFG {
static int evenNumSubstring(String str)
{
int len = str.length();
int count = 0 ;
for ( int i = 0 ; i < len; i++)
{
int temp = str.charAt(i) - '0' ;
if (temp % 2 == 0 )
count += (i + 1 );
}
return count;
}
public static void main(String args[])
{
String str= "1234" ;
System.out.println(evenNumSubstring(str));
}
}
|
Python3
def evenNumSubstring( str ):
length = len ( str )
count = 0
for i in range ( 0 ,length, 1 ):
temp = ord ( str [i]) - ord ( '0' )
if (temp % 2 = = 0 ):
count + = (i + 1 )
return count
if __name__ = = '__main__' :
str = [ '1' , '2' , '3' , '4' ]
print (evenNumSubstring( str ))
|
C#
using System;
public class GFG {
static int evenNumSubstring( string str)
{
int len = str.Length;
int count = 0;
for ( int i = 0; i < len; i++)
{
int temp = str[i] - '0' ;
if (temp % 2 == 0)
count += (i + 1);
}
return count;
}
public static void Main()
{
string str= "1234" ;
Console.Write(
evenNumSubstring(str));
}
}
|
Javascript
<script>
function evenNumSubstring(str)
{
let len = str.length;
let count = 0;
for (let i = 0; i < len; i++)
{
let temp = str[i] - '0' ;
if (temp % 2 == 0)
count += (i + 1);
}
return count;
}
let str= "1234" ;
document.write(evenNumSubstring(str));
</script>
|
PHP
<?php
function evenNumSubstring( $str )
{
$len = strlen ( $str );
$count = 0;
for ( $i = 0; $i < $len ; $i ++)
{
$temp = $str [ $i ] - '0' ;
if ( $temp % 2 == 0)
$count += ( $i + 1);
}
return $count ;
}
$str = "1234" ;
echo evenNumSubstring( $str ), "\n" ;
?>
|
Time Complexity: O(length of string).
This article is contributed by Anuj Chauhan.
Count Even and Odd Digits:
Approach:
Initialize variables e and o to 0, which will be used to count the number of even and odd digits in the string.
Loop through each character in the string s, and for each character:
a. Convert the character to an integer and check if it is even by taking its modulus with 2. If it is even, increment e by 1, otherwise increment o by 1.
Calculate the number of even and odd substrings using the formula (n*(n+1))/2, where n is the number of even or odd prefixes. This formula is derived from the fact that the number of substrings that can be formed from a string of length n is (n*(n+1))/2.
Add the number of even and odd substrings to get the total number of even substrings.
Return the total number of even substrings.
C++
#include <iostream>
#include <string>
using namespace std;
int count_even_substrings(string s) {
int n = s.length();
int e = 0;
for ( char c : s) {
if ((c - '0' ) % 2 == 0) {
e++;
}
}
int o = n - e;
return (e * (e + 1) / 2) + (o * (o + 1) / 2);
}
int main() {
string s = "1234" ;
cout << count_even_substrings(s) << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
static int countEvenSubstrings(String s) {
int n = s.length();
int e = 0 ;
for ( char c : s.toCharArray()) {
if ((c - '0' ) % 2 == 0 ) {
e++;
}
}
int o = n - e;
return (e * (e + 1 ) / 2 ) + (o * (o + 1 ) / 2 );
}
public static void main(String[] args) {
String s = "1234" ;
System.out.println(countEvenSubstrings(s));
}
}
|
Python3
def count_even_substrings(s):
n = len (s)
e = sum ( 1 for c in s if int (c) % 2 = = 0 )
o = n - e
return (e * (e + 1 ) / / 2 ) + (o * (o + 1 ) / / 2 )
s = "1234"
print (count_even_substrings(s))
|
C#
using System;
class Program {
static int CountEvenSubstrings( string s)
{
int n = s.Length;
int e = 0;
foreach ( char c in s)
{
if ((c - '0' ) % 2 == 0) {
e++;
}
}
int o = n - e;
return (e * (e + 1) / 2) + (o * (o + 1) / 2);
}
static void Main()
{
string s = "1234" ;
Console.WriteLine(
CountEvenSubstrings(s));
}
}
|
Javascript
function count_even_substrings(s) {
let n = s.length;
let e = [...s].filter(c => parseInt(c) % 2 == 0).length;
let o = n - e;
return (e*(e+1)/2) + (o*(o+1)/2);
}
let s = "1234" ;
console.log(count_even_substrings(s));
|
The time complexity of this algorithm is O(n), where n is the length of the input string s. This is because the algorithm iterates through the input string once to count the number of even and odd digits, and then performs two constant-time calculations to determine the number of even and odd substrings. The dominant operation in this algorithm is the iteration through the input string, which takes O(n) time.
The auxiliary space of this algorithm is O(1), because the algorithm only uses a constant amount of additional space to store the counts of even and odd digits. The amount of additional space used does not depend on the length of the input string. Therefore, the space complexity of this algorithm is constant.
Share your thoughts in the comments
Please Login to comment...