-1

I am getting a different value when I am performing the iteration manually on

"welcometojava"

when the value of i=5

I am getting following value of variable

when i=5;

substring =MET
smallest="com"
largest="ome"

At this point, the value that I am getting is not validating both the if conditions
I want help with the values of all 3 variables on each iteration so that I can know where I am getting wrong.Thankyou

public class Solution {

public static String getSmallestAndLargest(String s, int k) {
    String smallest = "";
    String largest = "";
    smallest = largest = s.substring(0, k);
    for (int i=1; i<s.length()-k+1; i++) {
        String substr = s.substring(i, i+k);
        if (smallest.compareTo(substr) > 0)
            smallest = substr;
        if (largest.compareTo(substr) < 0)
            largest = substr;
    }
    return smallest + "\n" + largest;
}
Amit
  • 44
  • 6
  • What is the problem? What did you expect and how is it different from the results you get? – TMG Feb 02 '20 at 16:34
  • You can just print the values of `substr`, `smallest`, and `largest` on each iteration? – PeMa Feb 02 '20 at 16:37
  • I am trying to understand this code by executing the code logic on paper – Amit Feb 02 '20 at 16:39
  • Your program is lexicographically compares current sub string(substr) with smallest and largest string, and it check that current sub string is smaller then existing smallest or it larger then exiting largest. What other configuration you have in this program? Both IF condition in loop get fails if "smallest < substr < largest" – Nils Feb 02 '20 at 16:57
  • Thankyou go the solution. – Amit Feb 02 '20 at 17:21

1 Answers1

1

If you are not using a debugger, you can trace the values by printing them e.g. I've added System.out.println("i=" + i + ", substring=" + substr + ", smallest=" + smallest + ", largest=" + largest); into your code as follows and also shown the output below it to help you understand how your code is behaving:

public class Solution {
    public static void main(String[] args) {
        getSmallestAndLargest("welcometojava", 3);
    }

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = "";
        String largest = "";
        smallest = largest = s.substring(0, k);
        for (int i = 1; i < s.length() - k + 1; i++) {
            String substr = s.substring(i, i + k);
            if (smallest.compareTo(substr) > 0)
                smallest = substr;
            if (largest.compareTo(substr) < 0)
                largest = substr;
            System.out.println("i=" + i + ", substring=" + substr + ", smallest=" + smallest + ", largest=" + largest);
        }
        return smallest + "\n" + largest;
    }
}

Output:

i=1, substring=elc, smallest=elc, largest=wel
i=2, substring=lco, smallest=elc, largest=wel
i=3, substring=com, smallest=com, largest=wel
i=4, substring=ome, smallest=com, largest=wel
i=5, substring=met, smallest=com, largest=wel
i=6, substring=eto, smallest=com, largest=wel
i=7, substring=toj, smallest=com, largest=wel
i=8, substring=oja, smallest=com, largest=wel
i=9, substring=jav, smallest=com, largest=wel
i=10, substring=ava, smallest=ava, largest=wel

In a similar way, you can add some more print statements e.g. to print the value of smallest.compareTo(substr) > 0 and so on to trace your code to a deeper level. However, I strongly recommend you use a debugger to do so.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110