HackerRank in a String!

Sort by

recency

|

1079 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

    Went this way around with Javascript:

    const HRS = "hackerrank";
    function hackerrankInString(s: string): string {
        let matching = 0;
        for (let i = 0; i < s.length && matching < HRS.length; i++) {
            if (s[i] === HRS[matching]) matching++;
        }
        return matching === HRS.length ? "YES" : "NO";
    }
    
  • + 0 comments

    Try the sript below on python and thnak me later:)

    def hackerrankInString(s):
        return "YES" if re.search(r'h.*a.*c.*k.*e.*r.*r.*a.*n.*k.*', s) else "NO"
    
  • + 1 comment

    def hackerrankInString(s): # Write your code here

    fs = 'hackerrank'
    if len(s) < len(fs):
            return 'NO'
    for i in range(len(fs)):
            for c in s:
                    if c == fs[i]:
                            i +=1
                    if i == len(fs):
                            return 'YES'
            return 'NO'
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM_)
    
    solve :: String -> String
    solve = go "hackerrank"
      where
        go [] _ = "YES"
        go _ [] = "NO"
        go (x : xs) (y : ys)
          | x == y = go xs ys
          | otherwise = go (x : xs) ys
    
    main :: IO ()
    main = do
      n <- readLn :: IO Int
      replicateM_ n $ getLine >>= putStrLn . solve