Type Here to Get Search Results !

Java Substring Comparisons | HackerRank Solution By CodingHumans |

0


Java Substring Comparisons 

Problem

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.

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]).

Output Format

Return the respective lexicographically smallest and largest substrings as a single newline-separated string.

Sample Input 0

welcometojava
3

Sample Output 0

ava
wel

Explanation 0

String s = "welcometojava"  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 given  in the hackerrank editor then prints ava as our first line of output and wel as our second line of output.





Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.


HAVE A GOOD DAY 😁






Solution:
( java )

import java.util.Scanner;

public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String currstr=s.substring(0,k);
        String smallest = currstr;
        String largest = currstr;
        for(int i=k;i<s.length();i++){
            currstr=currstr.substring(1,k)+s.charAt(i);
            if(largest.compareTo(currstr)<0){
                largest=currstr;
            }
            if(smallest.compareTo(currstr)>0){
                smallest=currstr;
            }
        }
        // Complete the function
        // 'smallest' must be the lexicographically smallest substring of length 'k'
        // 'largest' must be the lexicographically largest substring of length 'k'
        
        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));
    }
}

Post a Comment

0 Comments

Top Post Ad

Below Post Ad