HackerRank in a String!

Sort by

recency

|

1085 Discussions

|

  • + 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";
    }
    
  • + 0 comments
    def hackerrankInString(s):
        h='hackerrank1'
        j=0
        for i in s:
            if i==h[j]:
                j+=1
        if j==10:
            return 'YES'
        return 'NO'
    
  • + 0 comments

    Java:

    public static String hackerrankInString(String s) {
            
            String pattern = "hackerrank";
            int patternLength = pattern.length();
            int patternPointer = 0;
            
            for(int i = 0; i < s.length(); i++) {
                if(s.charAt(i) == pattern.charAt(patternPointer)) {
                    patternPointer++;
                    
                    if(patternPointer == patternLength) {
                        return "YES";
                    }
                }
            }
            
            return "NO";
        }
    
  • + 0 comments

    C++ Solution

    string hackerrankInString(string s) {
      string refS = "hackerrank";
      size_t preIt = 0;
      for(size_t i = 0; i < refS.length(); i++) {
        size_t it = s.find_first_of(refS[i], preIt);
        if(it == string::npos)
          return "NO";
        preIt = s.find_last_of(refS[i], it) + 1;
      }
      return "YES";
    }