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.
Similar Reads
Number of substrings divisible by 6 in a string of integers
Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when convert into integer are divisible by 6. Substring does not contain leading zeroes. Examples: Input : s = "606". Output : 5 Substrings "6", "0", "6", "60", "606" are divisible by 6. Input : s = "48
9 min read
Number of substrings divisible by 4 in a string of integers
Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when converted into integer are divisible by 4. Substring may contain leading zeroes. Examples: Input : "124" Output : 4 Substrings divisible by 4 are "12", "4", "24", "124" . Input : "04" Output : 3 Su
11 min read
Number of subsequences in a string divisible by n
Given a string consisting of digits 0-9, count the number of subsequences in it divisible by m.Examples: Input : str = "1234", n = 4Output : 4The subsequences 4, 12, 24 and 124 are divisible by 4. Input : str = "330", n = 6Output : 4The subsequences 30, 30, 330 and 0 are divisible by n.Input : str =
11 min read
Number of substrings with odd decimal value in a binary string
Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read
Number of sub-strings in a given binary string divisible by 2
Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive appr
4 min read
XOR of all substrings of a given Binary String
Given a binary string str of size N, the task is to calculate the bitwise XOR of all substrings of str. Examples: Input: str = "11"Output: 11Explanation: The substrings of "11" are: 1, 1, and 11.Their XOR = 1 â 1 â 11 = 11 Input: str = "110"Output: 111Explanation: The substrings of 110 are: 1, 1, 0,
6 min read
Find the longest Substring of a given String S
Given a string S of length, N. Find the maximum length of any substring of S such that, the bitwise OR of all the characters of the substring is equal to the bitwise OR of the remaining characters of the string. If no such substring exists, print -1. Examples: Input: S = "2347"Output: 3?Explanation:
10 min read
Count of repeating digits in a given Number
Given a number N, the task is to count the total number of repeating digits in the given number. Examples: Input: N = 99677 Output: 2Explanation:In the given number only 9 and 7 are repeating, hence the answer is 2. Input: N = 12Output: 0Explanation:In the given number no digits are repeating, hence
12 min read
Number of subsequences in a given binary string divisible by 2
Given binary string str of length N, the task is to find the count of subsequences of str which are divisible by 2. Leading zeros in a sub-sequence are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only subsequences which are divisible by 2.Input: str = "10010" Output: 22 Naiv
4 min read
Form minimum number of Palindromic Strings from a given string
Given a string S, the task is to divide the characters of S to form minimum number of palindromic strings. Note: There can be multiple correct answers. Examples: Input: S = "geeksforgeeks"Output: {eegksrskgee, o, f} Explanation: There should be at least 3 strings "eegksrskgee", "o", "f". All 3 forme
12 min read