HackerRank in a String!

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