1
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

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


            String curr = s.substring(i, i + k);
            if (smallest.compareTo(curr) > 0){
                smallest = curr;
            }
            if (largest.compareTo(curr) < 0) {
                largest = curr;
            }
        }


        return smallest + "\n" + largest;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        int k = scan.nextInt();
        scan.close();

        System.out.println(getSmallestAndLargest(s, k));
    }
}

The code should return the lexicographically smallest and largest substring, but it looks like I am going wrong somewhere which I couldn't identify.

Input (stdin):

welcometojava
3

Your Output (stdout):

com
wel

Expected Output:

ava
wel

3 Answers 3

1

Try this to get the last 3 string:

largest = s.substring(s.length -3); 
Sign up to request clarification or add additional context in comments.

Comments

0

It's because this loop:

for(int i=0;i<s.length()-k;i++){

Doesn't see the last three letters - you stop at the j, not the a. You can find this simply by debugging or by thinking about your terminating condition. You need:

for (int i = 0; i < s.length() - k + 1; i++) {

Comments

0

Follow this code

 public static String getSmallestAndLargest(String s, int k) {
        String smallest = "";
        String largest = "";

        char[] strArr = s.toCharArray();
        int size = strArr.length;

        char smallestChar = smallest_alphabet(strArr, size);
        char largestChar  = largest_alphabet(strArr, size);

        String[] smallestCharStringSet =  prepareSetOfString(s, k, smallestChar);
        String[] largestCharStringSet = prepareSetOfString(s, k, largestChar);

        smallest = smallestString(smallestCharStringSet);
        largest = largestString(largestCharStringSet);
        return smallest + "\n" + largest;
    }

    public static String[] prepareSetOfString(String s, int k, char c){
        int count = getCharCount(s,c);

        String[] possibleStringCollection  = new String[count];
        String operationString = s;
        possibleStringCollection[0] = operationString;
        for(int i=0; i< count; i++){
            int indexOfChar = operationString.indexOf(c+"");
            if(indexOfChar+k <= operationString.length()){
                possibleStringCollection[i] = operationString.substring(indexOfChar, indexOfChar+k);
                    operationString = operationString.substring(operationString.indexOf(c+"")+1);
            }
        }

        return possibleStringCollection;
    }

    public static String smallestString(String[] smallestCharStringSet){
        String smallestString = smallestCharStringSet[0];
        for(int i = 0; i < smallestCharStringSet.length; i++){
            if(smallestCharStringSet[i] != null)
                if(smallestString.compareTo(smallestCharStringSet[i]) > 0)
                {
                    smallestString = smallestCharStringSet[i];
                }
        }
        return smallestString;
    }

    public static String largestString(String[] largestCharStringSet){
        String largestString = largestCharStringSet[0];
        for(int i = 0; i < largestCharStringSet.length; i++){
            if(largestCharStringSet[i] != null)
                if(largestString.compareTo(largestCharStringSet[i]) < 0)
                {
                    largestString = largestCharStringSet[i];
                }
        }
        return largestString;
    }

    public static int getCharCount(String s, char c)
    {
        int count = 0;
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == c)
                count++;
        }
        return count;
    }

    public static char largest_alphabet(char a[], int n)
    {
        char max = 'A';

        for (int i=0; i<n; i++)
            if (a[i] > max)
                max = a[i];

        return max;
    }

    public static char smallest_alphabet(char a[], int n)
    {
        char min = 'z';

        for (int i=0; i<n-1; i++)
            if (a[i] < min)
                min = a[i];

        return min;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.