HackerRank in a String!

  • + 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()
        }
    }