We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The loop will run till the end of the s.length() even if we have already found our given subsequence.
For example for above variables, consider s = "hraaa" and str = "hr", the loop will go through 5 iterations even when we found our subsequence after 2, so better to put an if condition inside loop checking if ( j == str.length) and return "YES" and return "NO" outside loop in case the subsequence is not found.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
HackerRank in a String!
You are viewing a single comment's thread. Return to all comments →
Saving some computational time.
The loop will run till the end of the s.length() even if we have already found our given subsequence.
For example for above variables, consider s = "hraaa" and str = "hr", the loop will go through 5 iterations even when we found our subsequence after 2, so better to put an if condition inside loop checking if ( j == str.length) and return "YES" and return "NO" outside loop in case the subsequence is not found.