HackerRank in a String!

HackerRank in a String!

We say that a string contains the word hackerrank if a subsequence of its characters spell the word hackerrank. Remeber that a subsequence maintains the order of characters selected from a sequence.

More formally, let p[0], p[1], … , p[9] be the respective indices of h, a, c, k, e, r, r, a, n, k in string s. If p[0] < p[1] < p[2] < … < p[9] is true, then s contains hackerrank.

For each query, print YES on a new line if the string contains hackerrank, otherwise, print NO.

Example

s = haacckkerrannkk

This contains a subsequence of all of the characters in the proper order. Answer YES

s = haacckkerannk

This is missing the second ‘r’. Answer NO.

s = hccaakkerrannkk

There is no ‘c’ after the first occurrence of an ‘a’, so answer NO.

Function Description

Complete the hackerrankInString function in the editor below.

hackerrankInString has the following parameter(s):

  • string s: a string

Returns

  • string: YES or NO

Input Format

The first line contains an integer q, the number of queries.
Each of the next q lines contains a single query string s.

Constraints

  • 2 <= q <= 102

  • 10 <= length of s <= 104

Sample Input 0

1
2
3
2
hereiamstackerrank
hackerworld

Sample Output 0

1
2
YES
NO

Explanation 0

We perform the following q = 2 queries:

  1. s = hereiamstackerrank
    The characters of hackerrank are bolded in the string above. Because the string contains all the characters in hackerrank in the same exact order as they appear in hackerrank, we return YES.

  2. s = hackerworld does not contain the last three characters of hackerrank, so we return NO.

Sample Input 1

1
2
3
2
hhaacckkekraraannk
rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt

Sample Output 1

1
2
YES
NO

Solution

Solution 1 with JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* Complete the 'hackerrankInString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/

function hackerrankInString(s) {
// Write your code here
const value = 'hackerrank';

const count = (s.split('') || []).reduce((target, item, index) => {
value[target] === s[index] && target++;

return target;
}, 0);

return count === value.length ? 'YES' : 'NO';
}

Solution 2 with TS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
* Complete the 'hackerrankInString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/

function hackerrankInString(s: string): string {
// Write your code here
const value = 'hackerrank';

const count = (s.split('') || []).reduce(
(target: number, item: string, index: number): number => {
value[target] === s[index] && target++;

return target;
},
0
);

return count === value.length ? 'YES' : 'NO';
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×