HackerRank in a String!

Sort by

recency

|

1099 Discussions

|

  • + 0 comments
    public static String hackerrankInString(String s) {
        String str = "hackerrank";
        for(int i = 0; i < str.length(); i++) {
            int index = s.indexOf(str.charAt(i));
            if(index == -1) return "NO";
            s = s.substring(index+1);
        }
        return "YES";
    }
    
  • + 0 comments
    string hackerrankInString(string s) {
        string searchStr = "hackerrank";
        auto it = searchStr.begin();
        for (int i=0; i<s.size(); ++i) {
            if(s[i] == *it)
                ++it;
        }
        if(it == searchStr.end())
            return "YES";
        return "NO";
    }
    
  • + 0 comments
    def hackerrankInString(s):
        # Write your code here
        a = ['h', 'a', 'c', 'k', 'e', 'r', 'r', 'a', 'n', 'k']
        
        end = len(s) - 1
        
        while end >= 0:
            if len(a) < 1:
                break
            current = s[end]
            current_match = a.pop()
            if current != current_match:
                a.append(current_match)
            end -= 1
                
        return "NO" if len(a) > 0 else "YES"
    
  • + 0 comments

    Here is a O(n) time and O(1) time solution in python:

    def hackerrankInString(s):
        hacker = 'hackerrank'
        i = 0
    
        for c in s:
            if c == hacker[i]:
                i += 1
                    
            if i == len(hacker):
                return "YES"
            
        return "NO"
    
  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-running-time-of-algorithms-problem-solution.html