HackerRank Saying Hi Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Saying Hi 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.

HackerRank Saying Hi Solution
HackerRank Saying Hi Solution

HackerRank Saying Hi Solution


Given a sentence, s, write a RegEx to match the following criteria:

  1. The first character must be the letter H or h.
  2. The second character must be the letter I or i.
  3. The third character must be a single space (i.e.: \s).
  4. The fourth character must not be the letter D or d.

Given n lines of sentences as input, print each sentence matching your RegEx on a new line.

Input Format

The first line contains an integer, n, denoting the number of lines of sentences.
Each of the n subsequent lines contains some sentence s you must match.


  • 1 <= n <= 10
  • Each sentence, s, contains 1 to 10 words.
  • Each word/token in a sentence is comprised only of upper and lowercase English letters.

Output Format

Find each sentence, s, satisfying the RegEx criteria mentioned above, and print it on a new line.

Sample Input

Hi Alex how are you doing
hI dave how are you doing
Good by Alex
hidden agenda
Alex greeted Martha by saying Hi Martha

Sample Output

Hi Alex how are you doing


The first sentence satisfies the RegEx criteria set forth in the Problem Statement (starts with the case-insensitive word Hi, followed by a space, followed by a letter that is not d), so we print the sentence on a new line.

The second sentence fails our RegEx criteria, as the second word/token starts with a d (so we print nothing).

The third sentence fails our RegEx criteria, as it doesn’t start with an h (so we print nothing).

The fourth sentence fails our RegEx criteria, as the third character in the sentence is not a space (so we print nothing).

The fifth sentence fails as our RegEx criteria, as the sentence does not start with the word Hi (so we print nothing).

HackerRank Saying Hi Solution in Cpp

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

int main()
    int n;
    string s;

    cin >> n;
    for (int i = 0; i < n; ++i)
        if ((s[0] == 'h' || s[0] == 'H') && (s[1] == 'i' || s[1] == 'I') && s[2] == ' ' &&
            (s[3] != 'd' && s[3] != 'D'))
            cout << s << endl;
    return 0;

HackerRank Saying Hi Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt(); sc.nextLine();
    for (int i = 0; i < n; i++) {
      String line = sc.nextLine();
      String copy = (line + "").toLowerCase();
      if ( copy.startsWith("hi ") && (copy.length() < 4 || copy.charAt(3) != 'd') ){

HackerRank Saying Hi Solution in Python

n = input()
for i in xrange(n):
    line  = raw_input()
    if(line[:2].upper() == "HI" and line[2].isspace() and line[3].upper()!= "D"):
        print line

HackerRank Saying Hi Solution in JavaScript

process.stdin.on("data", function (input) {
	input = input.match(/^(.*)$/igm);
	var n = parseInt(input[0]),
		strs = input.slice(1,n+1);
	for (i=0, j=strs.length; i<j; i+=1) {
		if (strs[i].match(/^hi\s+?[^d]/ig)) {

HackerRank Saying Hi Solution in 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 = trim(fgets($_fp));
    $search = '/^hi\s[^d]/i';
    print preg_match($search, $line) ? $line . PHP_EOL : '';

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

Sharing Is Caring