HackerRank in a String!

Sort by

recency

|

1088 Discussions

|

  • + 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"; }
    }
    
  • + 0 comments

    My answer in Typescript, simple, not minimized

    function hackerrankInString(s: string): string {
        /**
         * idea is simple
         * 
         * 1. create 2 array of 'hackerrank' and 's' that hold it characters
         * 2. loop ...
         *      reduce [s]
         *      reduce [hackerrank] too if both starting element is same
         *      if [hackerrank] empty first, go 'YES'
         *      if [s] empty first, go 'NO
         */
    
        let _h = 'hackerrank'.split('')
        let _s = s.split('')
    
        while (true) {
            if (_h.length == 0) return 'YES'
            if (_s.length == 0) return 'NO'
    
            _s[0] == _h[0] && _h.shift()
            _s.shift()
        }
    }
    
  • + 0 comments

    Simple Java:

    public static String hackerrankInString(String s) {
        String str = "hackerrank";
        int i = 0; // Pointer for the "hackerrank" string
    
        for (int j = 0; j < s.length(); j++) {
            if (i < str.length() && str.charAt(i) == s.charAt(j)) {
                i++; // Move the pointer for "hackerrank"
            }
            if (i == str.length()) { // Check if all characters of "hackerrank" are found
                return "YES";
            }
        }
    
        return "NO"; // If the loop ends and "hackerrank" is not fully matched
    }
    
  • + 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";
    }