← Back to challenges

Count Substring

JavaScriptHardstringsloops

Instructions

Implement a function count_substring that counts the number of substrings that begin with character "A" and ends with character "X".

For example, given the input string "CAXAAYXZA", there are four substrings that begin with "A" and ends with "X", namely: "AX", "AXAAYX", "AAYX", and "AYX".

Examples

countSubstring("CAXAAYXZA") ➞  4

countSubstring("AAXOXXA") ➞ 6

countSubstring("AXAXAXAXAX") ➞ 15

Notes

  • You should aim to avoid using nested loops to complete the task.
  • You can assume that the input string is composed of English upper case letters only.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.