- Related Questions & Answers
- Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
- Count substrings with each character occurring at most k times in C++
- Check if string ends with desired character in JavaScript
- Count of pairs (x, y) in an array such that x < y in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count of sub-strings that contain character X at least once in C++
- Find if a string starts and ends with another given string in C++
- Count number of substrings with numeric value greater than X in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- Find larger of x^y and y^x in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Program to build DFA that starts and ends with ‘a’ from the input (a, b)
- Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character
- Count substrings with same first and last characters in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given string str. The goal is to count the number of substrings in str that have starting character same as character X and ending character same as character Y. For example, if input is “artact” and X=’a’ and Y=’t’, the substrings will be “art”, “act”, “artact”. The count is 3.

Let us understand with examples.

**Input** − str=”abcccdef” X=’a’ Y=’c’

**Output** −Count of substrings that starts with character X and ends with Y is − 3

**Explanation** − Substrings will be

“abc”, “abcc”, “abccc”. Total 3.

**Input** − str=”tempest” X=’t’ Y=’t’

**Output** − Count of substrings that starts with character X and ends with Y are − 3

**Explanation** − Substrings will be −

“t” , “tempest” , “t”. Total 3

We will traverse the string and increment count of X. If then Y is encountered add X count to the count of strings that start with X and end with Y.

Take string str. Calculate the length as str.size().

Function X_Y(string str, int length, char X, char Y) takes string str, characters X, Y and returns the count of substrings of str that start with X and end with Y.

Take the initial count as 0.

Take x_total as count of character X in str.

Traverse str using for loop. From i=0 to i<length.

If str[i]==X, increment count of X’s in str (x_total++).

If str[i]==Y, add x_total to count. If no X was there x_total would be 0, otherwise, Y is the end character of the substring that started from X.

Return count as the desired result.

#include <bits/stdc++.h> using namespace std; int X_Y(string str, int length, char X, char Y){ int count = 0; int x_total = 0; for (int i = 0; i < length; i++){ if(str[i] == X){ x_total++; } if (str[i] == Y){ count = count + x_total; } } return count; } int main(){ string str = "defaabbcchhkl"; int length = str.size(); char X = 'd'; char Y = 'a'; cout<<"Count of substrings that starts with character X and ends with character Y are: "<<X_Y(str, length, X, Y); return 0; }

If we run the above code it will generate the following output −

Count of substrings that starts with character X and ends with character Y are: 2

Advertisements