Java Substring Comparisons Solution


Problem Overview

Original Problem: Java Substring Comparisons | Hackerrank

Difficulty: Easy

Category: Strings

Description: We are given a string and a length. Our task is to find and return the lexicographically smallest and largest substrings of the original string of the provided length.

Understanding the Problem

This problem focuses on two main components: creating substrings and comparing the substrings so that the smallest and largest substrings can be identified.

Input:

Two lines of text

  • A string up to 1000 characters in length

  • An integer up to the maximum length of the string

Output:

Two lines of text:

  • The lexicographically smallest substring of the provided length

  • The lexicographically largest substring of the provided length

Initial Reasoning

The provided template already handles reading input, so our job is to complete the logic inside the getSmallestAndLargest() method.

Since we need to examine every substring of the provided length, we can loop through the string, extract each substring, and compare it with the current smallest and largest substrings using Java's built-in compareTo() method.

Pseudocode

// getSmallestAndLargest function: 
Create variables to store the current smallest and largest substrings
For each possible substring of the provided length
	If it is smaller than the current smallest substring
		Update the smallest substring to the current substring
	If it is larger than the current smallest substring
		Update the largest substring to the current substring 
Return the smallest and largest substrings

Solution Code

// Note this is one of many possible solutions
import java.util.Scanner;

public class Solution {

    public static String getSmallestAndLargest(String inputString, int len) {
        // Assume the first substring is both the smallest and largest initially
        String smallest = inputString.substring(0, len);
        String largest = inputString.substring(0, len);

        // Loop through the string to find all possible substrings
        for (int i = 1; i <= inputString.length() - len; i++) {
            String substring = inputString.substring(i, i + len);

            // Update smallest if the new substring is smaller
            if (substring.compareTo(smallest) < 0) {
                smallest = substring;
            }

            // Update largest if the new substring is larger
            if (substring.compareTo(largest) > 0) {
                largest = substring;
            }
        }

        // Return the results in the required format
        return smallest + "\n" + largest;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String inputString = scan.next();
        int len = scan.nextInt();
        scan.close();

        System.out.println(getSmallestAndLargest(inputString, len));
    }
}

Commenting on Alterations

While we have done our best to maintain similarity to the template code provided in the original problem, the following changes have been made to follow best practices:

  • Renamed variables:

    Using more descriptive names improves the clarity of the code and makes it easier for anyone reading the code to understand what each variable represents.