Exercice in java but the logic apply need help

Hello, i’m trying to do this exercice:

My code is:

public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	String s = "welcometojava";
    	int sLength = s.length();
//    	System.out.println("Substring: "+ s.substring(0, 3));
//    	System.out.println("Substring: "+ s.substring(1, 4));
//    	System.out.println("Substring: "+ s.substring(2, 5));
//    	System.out.println("Substring: "+ s.substring(3, 6));
//    	System.out.println("Substring: "+ s.substring(4, 7));
//    	System.out.println("Substring: "+ s.substring(5, 8));
//    	System.out.println("Substring: "+ s.substring(6, 9));
//    	System.out.println("Substring: "+ s.substring(7, 10));
//    	System.out.println("Substring: "+ s.substring(8, 11));
//    	System.out.println("Substring: "+ s.substring(9, 12));
//    	System.out.println("Substring: "+ s.substring(10, 13));

    	int a = 0;
    	int b = 3;
    	int copyB = b;
    	String smallest = s.substring(0, b);
    	String biggest = s.substring(0, b);

    	System.out.println("a: " +a);
        System.out.println("B: " +b+"\n");

        for(int k = 0; k < sLength-copyB; k++) {
    		if (s.substring(a, b).compareTo(smallest) < 0){	
        		smallest = s.substring(a, b);
        	}else if(s.substring(a, b).compareTo(biggest) > 0) {	
        		biggest = s.substring(a, b);
        	b += 1;
        	a += 1;
//          System.out.println("Smallest "+ smallest + "Biggest "+ biggest);
        	System.out.println("a: " +a);
            System.out.println("B: " +b+"\n");
        System.out.println("Smallest "+ smallest);
        System.out.println("Biggest "+ biggest);

Some thing must be wrong in my if statement and i’m not using compareTo propely (maybe the first initiation variables ?).


Your function should not have the examples hardcoded in anyway, it should have the string and k values as inputs - modify the function provided for you on the other site!

By that I mean b and s shouldn’t have hardcoded values. copyB is entirely pointless too - just use k which should be a parameter to the function!

Your output format is wrong too, it needs to print out just the strings each on a seperate line, without any prefix

Looping over k and manually incrementing a and b seems a little pointless too, why not loop over (a = 0; a <= s.length() - k; a++) and have s.substring(a, a+k)?

Otherwise the logic looks “fine” to me

Please do note that it’s not very likely that people answer java questions here

Hello, i 100% know about the hardcoded part. I just tried in my IDE to work it out so i made it simple to avoid typing stuff all over and over again. I’m not trying to pass the test with this, i’m trying to make it work so i can accomodate it to suit the requirement form.
But it clearly doesn’t work :confused: And this is the only place when i’m familiar with the forum

As a sanity check I just implemented similar on HR and it succeeded all the tests

What’s going wrong when you’re looking at it locally? As in what’s wrong with the output?

I don’t have a java compiler on hand to test it myself, sorry

1 Like

When i run my code, i have this output:

Smallest com
Biggest wel

It should be “ava” and not com

Ahh yes, I had at first - notice that ava is the final 3 letters of the string…

Your loop misses out the final part :slight_smile:

1 Like

I will try to find out why, thx

Hello, i did it:

	public static String getSmallestAndLargest(String s, int k) {
		int sLength = s.length() - k + 1;
		String smallest = s.substring(0, k);
		String largest = s.substring(0, k);

	    for(int i = 0; i < sLength; i++) {
			if (s.substring(i, k).compareTo(smallest) < 0){	
	    		smallest = s.substring(i, k);
	    	}else if(s.substring(i, k).compareTo(largest) > 0) {	
	    		largest = s.substring(i, k);
	    	k += 1;

	    return smallest + "\n" + largest;

	public static void main(String[] args) {
	    Scanner scan = new Scanner(System.in);
	    String s = scan.next();
	    int k = scan.nextInt();
	    System.out.println(getSmallestAndLargest(s, k));

My only concern is i’m not sure why i have to do this int sLength = s.length() - k + 1;
For the 'welcometojava' exemple, i know i have to get 11 to make it work. So i tried this and it gave me 11 xD
And it work for the rest so it was the right move but i don’t know, really why.
Have a nice day

public String substring(int beginIndex, int endIndex)

In this beginIndex starts at 0 when u want to start at beginning of string. So for the last leg of the for loop you need beginIndex to be 10 so that you get 10thindexletter+11indexthletter+12thindexletter.

In your “for loop”, the last loop is for 10 as you have specified ( i < 11). (sLength being 13-3+1 = 11). If you did a “<= sLength” for the for loop, you would be fine as well without the +1.

Hope this clarifies.