Given a string of digits 0 – 9. The task is to count number of substrings which when convert into integer form an even number.
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, substring must end wit 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 index of that even digit plus one.
Time Complexity: O(length of string).
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Counting even decimal value substrings in a binary string
- Check if a string can be split into even length palindromic substrings
- Minimum digits to be removed to make either all digits or alternating digits same
- Count of substrings consisting of even number of vowels
- Check if a number with even number of digits is palindrome or not
- Smallest odd number with even sum of digits from the given number N
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Given a binary string, count number of substrings that start and end with 1.
- Sum of all substrings of a string representing a number | Set 1
- Number of substrings divisible by 6 in a string of integers
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Number of substrings divisible by 4 in a string of integers
- Number of substrings with odd decimal value in a binary string
- Number of substrings of a string
- Number of substrings of one string present in other
- Rearrange the string to maximize the number of palindromic substrings
- Count the number of vowels occurring in all the substrings of given string
- Repeat substrings of the given String required number of times