HackerRank in a String!

  • + 0 comments

    One way to improve it would to possibly put the return yes inside the for loop so you dont do any unneeded iterations because right now even if its already confirmed that it contains "hackerrank" in order it will continue through the rest of String s. possibly do this inside the loop instead.

    for (int i = 0; i < s.length(); i++) {
                if (j <= str.length()){ 
                    if(s.charAt(i) == str.charAt(j)) {
                        j++;
                } else {
                        return "YES";
                    }
            }
    }
            return "NO";
    

    since it is the same amount of actions as combining the if statement and less if the j integer is already equal to the str length as well as adding a little more power to it.

    but i love the

    if (s.length() < str.length()) {
                return "NO";
            }
    

    I wish i would have thought of including that in my algorithm