Java Substring Comparisons

  • + 0 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.