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.
Your for loop never gets run when the substring length is equal to the input string length. The condition of the for loop should be
i < s.length() - k + 1
The for loop should keep incrementing 'i' as long as 'i' is less than the (total string length minus the length of the substring plus 1).
Also your use of variable y will cause an issue when the substring is as long as the input string. Instead when calculating the substring use
s.substring(i, i + k)
Lastly rather than initialising your min and max values with 'aaa' and 'zzz', set them to '' and add a check in your for loop that sets min and max to the substring if this is the first time the loop has been run.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Substring Comparisons
You are viewing a single comment's thread. Return to all comments →
Your for loop never gets run when the substring length is equal to the input string length. The condition of the for loop should be
i < s.length() - k + 1
The for loop should keep incrementing 'i' as long as 'i' is less than the (total string length minus the length of the substring plus 1).
Also your use of variable y will cause an issue when the substring is as long as the input string. Instead when calculating the substring use
s.substring(i, i + k)
Lastly rather than initialising your min and max values with 'aaa' and 'zzz', set them to '' and add a check in your for loop that sets min and max to the substring if this is the first time the loop has been run.