Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Counting Strings problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Counting Strings problem solution, Alice got a message M. It is in an alien language. A string in an alien language is said to be valid if it contains the letter a or z. Alice decided to count the number of valid substrings of the message M. Help him to do this. Two substrings are different if it occurs at different positions in the message.
 
 
HackerEarth Counting Strings problem solution

 

 

HackerEarth Counting Strings problem solution.

T = int(input())

for _ in range(T):

def nextOccurrence(M, n, start, ch1, ch2):

for i in range(start, n):

if (M[i] == ch1 or M[i] == ch2):

return i

return -1
def countSubStr(M, n, ch1, ch2):

cnt = 0

j = nextOccurrence(M, n, 0, ch1, ch2)

for i in range(n):

while (j != -1 and j < i):
j = nextOccurrence(M, n, j + 1, ch1, ch2)

if (j == -1):

break

cnt += (n - j)

return cnt

M = str(input())
n = len(M)
ch1 = "a"
ch2 = "z"
print(countSubStr(M, n, ch1, ch2))
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes