HackerRank in a String!

Sort by

recency

|

1090 Discussions

|

  • + 0 comments

    My easy c++ solution, here is the explanation : https://youtu.be/N3lhtXtqIoU

    string hackerrankInString(string s) {
        string target ="hackerrank";
        int currentIndex = 0;    
        for(int i = 0; i < s.size(); i++){
            if(s[i] == target[currentIndex]){
                currentIndex++;
                if(currentIndex == target.size()) return "YES";
            }
        }
        return "NO";
    }
    
  • + 0 comments

    Here is my Python solution!

    def hackerrankInString(s):
        index = 0
        string = "hackerrank"
        for i in range(len(s)):
            if s[i] == string[index]:
                index += 1
            if index == len(string) - 1:
                return "YES"
        return "NO"
    
  • + 0 comments

    Simple solution in Typescript with regex

    function hackerrankInString(s: string): string {
    	return !!s.match(/h.*a.*c.*k.*e.*r.*r.*a.*n.*k.*/)
    		? 'YES'
    		: 'NO';
    }
    
  • + 0 comments

    Python solution:

    def hackerrankInString(s):
        # Write your code here
        compare_to = "hackerrank"
        input_word = [] 
        index = 0
        for c in s:
            if len(input_word) != len(compare_to):  
                if c == compare_to[index]:
                    input_word.append(c)
                    index += 1
        if "".join(input_word) == compare_to:
            return "YES"
        else:
            return "NO"
    
  • + 0 comments

    Perl solution:

    sub hackerrankInString {
        my $str = shift;
        my @s = split("", $str);
        my @t = split("", "hackerrank");
        my $pos = -1;
        my @res_arr;
        
        for (my $i = 0; $i <= scalar(@t) - 1; $i++) {
            for (my $j = 0; $j <= scalar(@s) - 1; $j++) {
                if ($t[$i] eq $s[$j] && $pos < $j) {
                    $pos = $j;
                    push(@res_arr, $s[$j]);
                    last;
                }            
            }
        }
    
        if (join("", @res_arr) eq "hackerrank") {
            return "YES";
        } else { return "NO"; }
    }