Substring Comparisons in Java: HackerRank Solution

Java Substring Comparisons - HackerRank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Substring Comparisons Problem with a very easy explanation. This is the 16th problem of Java on HackerRank. In this article, you will get more than one approach to solve this problem. So let's start-

Java Substring Comparisons - Hacker Rank Solution


{tocify} $title={Table of Contents}

HackerRank Substring Comparisons - Problem Statement

We define the following terms:

Lexicographical Order, also known as alphabetic or dictionary order, orders characters as follows:

A < B < .... < Y < Z < a < b < ..... < y < z

For example, ball < cat, dog < dorm, Happy < happy, Zoo < ball.

A substring of a string is a contiguous block of characters in the string. For example, the substrings of abc are a, b, c, ab, bc, and abc.

Given a string, s, and an integer, k, complete the function so that it finds the lexicographically smallest and largest substrings of length k.

Function Description

Complete the getSmallestAndLargest function in the editor below.

getSmallestAndLargest has the following parameters:

  • string s: a string
  • int k: the length of the substrings to find

Returns

  • string: the string ' + "\n" + ' where and are the two substrings

Input Format

The first line contains a string denoting s.
The second line contains an integer denoting k.

Constraints

  • 1 <= |s| <= 1000
  • s consists of English alphabetic letters only (i.e., [a-zA-Z]).

Sample Input

welcometojava 3

Sample Output

ava wel

Explanation

String s"Welcome to Java" has the following lexicographically-ordered substrings of length k = 3:

["ava",  "com", "elc", "eto", "jav", "lco", "met", "oja", "ome", "toj", "wel" ]

We then return the first (lexicographically smallest) substring and the last (lexicographically largest) substring as two newline-separated values (i.e., ava\nwel).

The stub code in the editor then prints ava as our first line of output and wel as our second line of output.

Java Substring Comparisons - Hacker Rank Solution

Approach I:

import java.util.Scanner;

public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = "";
        String largest = "";
        
        java.util.List<String> a = new java.util.ArrayList<>();

        for(int i=0;i<s.length()-k+1;i++){
                a.add(s.substring(i,i+k));   
        }
        java.util.Collections.sort(a);
         smallest = a.get(0);
         largest = a.get(a.size()-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();
        scan.close();
      
        System.out.println(getSmallestAndLargest(s, k));
    }
}


Approach II:

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

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        int k = scanner.nextInt();
        String minSubstring = line.substring(0,k);
        String maxSubstring = line.substring(0,k);
        for (int i = 1; i < line.length()-k+1; i++) {
            String sub = line.substring(i,i+k);
            if (sub.compareTo(minSubstring) < 0) {
                minSubstring = sub;
            }
            if (sub.compareTo(maxSubstring) > 0) {
                maxSubstring = sub;
            }
        }
        System.out.println(minSubstring);
        System.out.println(maxSubstring);
    }
}

Disclaimer: The above Problem ( Java Substring Comparisons ) is generated by Hackerrank but the Solution is Provided by fixxgroup. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these two solutions will pass all the test cases. Now visit Java Substring Comparisons HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post