Counting String Subsequences
Description:
With your birthday coming up soon, your eccentric friend sent you a message to say "happy birthday":
hhhappyyyy biirrrrrthddaaaayyyyyyy to youuuu
hhapppyyyy biirtttthdaaay too youuu
happy birrrthdayy to youuu
happpyyyy birrtthdaaay tooooo youu
At first it looks like a song, but upon closer investigation, you realize that your friend hid the phrase "happy birthday" thousands of times inside his message. In fact, it contains it more than 2 million times! To thank him, you'd like to reply with exactly how many times it occurs.
To count all the occurences, the procedure is as follows: look through the paragraph and find a 'h'
; then find an 'a'
later in the paragraph; then find an 'p'
after that, and so on. Now count the number of ways in which you can choose letters in this way to make the full phrase.
More precisely, given a text string, you are to determine how many times the search string appears as a sub-sequence of that string.
Write a function called countSubsequences
that takes two arguments: needle
, the string to be search for and haystack
, the string to search in. In our example, "happy birthday"
is the needle and the birthday message is the haystack. The function should return the number of times needle
occurs as a sub-sequence of haystack
. Spaces are also considered part of the needle.
Since the answers can be very large, return only the last 8 digits of the answer in case it exceeds 8 digits. The answers to the test cases will all be shorter than 8 digits.
Similar Kata:
Stats:
Created | Feb 9, 2014 |
Published | Feb 9, 2014 |
Warriors Trained | 3388 |
Total Skips | 524 |
Total Code Submissions | 4183 |
Total Times Completed | 669 |
JavaScript Completions | 270 |
Python Completions | 399 |
Rust Completions | 27 |
Total Stars | 140 |
% of votes with a positive feedback rating | 95% of 120 |
Total "Very Satisfied" Votes | 110 |
Total "Somewhat Satisfied" Votes | 8 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 10 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 8 kyu |