Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Counting Strings problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post
CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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