HackerRank in a String!

  • + 0 comments

    Yes sure.

    Assume our string is "papahopackerank" So we will be looping the above string

    for i in "papahopackerank"
    

    Initially a = 0 means "hackerrank"[0] = "h"

    In our for loop once we find a "h" we will increase a. "papa" is completely ignored

    After finding "h" it will increase a and now "hackerrank"[1] = "a" which means it will start searching "a" in rest of the string then "c" then "k" up to the end.

    if a becomes 9 we can just break the loop and print "YES". Since a = 9 means we have found all letters of hackerrank.

    In our example string, a will become 5 since it will match only up to "hacker". So our program will print "No"