Find HackerRank Solution

Hello Programmers, In this post, you will learn how to solve Find HackerRank Solution. This problem is a part of the Regex HackerRank Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the  Regex HackerRank Solutions using  CPP, JAVA, PYTHON, JavaScript & PHP Programming Languages.

Find HackerRank Solution

Problem

At HackerRank, we always want to find out how popular we are getting every day and have scraped conversations from popular sites. Each conversation fits in 1 line and there are N such conversations. Each conversation has at most 1 word that says hackerrank (all in lowercase). We would like you to help us figure out whether a conversation:

1. Starts with hackerrank
2. Ends with hackerrank
3. Start and ends with hackerrank

Input Format

First line of the input contains an integer, N. Then N lines follow.
From the second line onwards, each line contains a set of W words separated by a single space

Constraints

• 1 <= N <= 10
• 1 <= W <= 100
• All the characters in W are lowercase alphabet characters.
• If C is the count of the characters in W, then 1 <= C <= 20

Output Format

For every line,

1. Print 1 if the conversation starts with hackerrank
2. Print 2 if the conversation ends with hackerrank
3. Print 0 if the conversation starts and ends with hackerrank
4. Print -1 if none of the above.

Sample Input

4
i love hackerrank
hackerrank is an awesome place for programmers
hackerrank
i think hackerrank is a great place to hangout

Sample Output

2
1
0
-1

Explanation

The first conversation ends with hackerrank and hence 2
The second conversation starts with hackerrank and hence 1
The third conversation has only one word, it starts and ends with hackerrank and hence 0.
The fourth conversation satisfies none of the above properties and hence 1.

Viewing Submissions

You can view others submissions if you solve this challenge. Navigate to the challenge leaderboard.

Find HackerRank Solution in Cpp

```#include <iostream>
#include <string>
using namespace std;

int main()
{
int n;
string s;

cin >> n;
cin.ignore();
for (int i = 0; i < n; ++i)
{
bool start = false;
bool end = false;
getline(cin,s);
if(s[0] == 'h' && s[1] == 'a' && s[2] == 'c' && s[3] == 'k' && s[4] == 'e'
&& s[5] == 'r' && s[6] == 'r' && s[7] == 'a' && s[8] == 'n' && s[9] == 'k')
start = true;
int t = s.length();
if(s[t-10] == 'h' && s[t-9] == 'a' && s[t-8] == 'c' && s[t-7] == 'k' && s[t-6] == 'e'
&& s[t-5] == 'r' && s[t-4] == 'r' && s[t-3] == 'a' && s[t-2] == 'n' && s[t-1] == 'k')
end = true;
if(start && end)
{
cout << 0 << endl;
continue;
}
if(start)
cout << 1 << endl;
else if (end)
cout << 2 << endl;
else
cout << -1 << endl;
}
return 0;
}```

Find HackerRank Solution in Java

```import java.util.Scanner;
public class Solution {
public static void main(String args[])
{
String input = "";
String pattern = "hackerrank";
for( int i= 0; i < nTestCases; i++)
{
if( input.startsWith(pattern) && input.endsWith(pattern) )
{
System.out.println("0");
}
else if( input.startsWith(pattern))
{
System.out.println("1");
}
else if( input.endsWith(pattern))
{
System.out.println("2");
}
else
{
System.out.println("-1");
}
}

}
}```

Find HackerRank Solution in Python

```import re
t=input()
for _ in range(t):
s = raw_input()
o=re.search("^hackerrank", s)
o2=re.search("hackerrank\$", s)
if o == None:
if o2 == None:
print -1
else:
print 2
else:
if o2 == None:
print 1
else:
print 0```

Find HackerRank Solution in JavaScript

```process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) {
input  = input.split("\n").slice(1);
result = [];

input.forEach(function(value, key) {
result[key] = -1;

if(/^hackerrank/.test(value))  result[key] = 1;
if(/hackerrank\$/.test(value))  result[key] = 2;
if(/^hackerrank\$/.test(value)) result[key] = 0;
});

console.log(result.slice(0,-1).join("\n"));
});```

Find HackerRank Solution in PHP

```<?php
\$_fp = fopen("php://stdin", "r");
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
fscanf(\$_fp, "%d", \$m);
for (\$i = 0; \$i < \$m; \$i++) {
\$line = fgets(\$_fp);
\$search = 'hackerrank';
if (preg_match('/^' . \$search . '/', \$line) && preg_match('/' . \$search . '\$/', \$line)) print 0 . PHP_EOL;
elseif (preg_match('/^' . \$search . '/', \$line)) print 1 . PHP_EOL;
elseif (preg_match('/' . \$search . '\$/', \$line)) print 2 . PHP_EOL;
else print -1 . PHP_EOL;
}```

Disclaimer: This problem (Find HackerRank) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

Sharing Is Caring