Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Substring Comparisons-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Substring Comparisons – Hacker Rank Solution
Java Substring Comparisons – Hacker Rank Solution
Objective
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
area
,b
,c
,ab
,bc
, andabc
.
Given a string,8, 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 8.
The second line contains an integer denoting k.
Constraints
- 8 consists of English alphabetic letters only (i.e.,
[a-zA-Z]
).
Sample Input 0
welcometojava 3
Sample Output 0
ava wel
Java Substring Comparisons – Hacker Rank Solution
import java.util.Scanner; public class Solution { public static void main(String[] args) { /* Save input */ Scanner scan = new Scanner(System.in); String s = scan.nextLine(); int k = scan.nextInt(); scan.close(); /* Create smallest and largest strings and initialize them */ 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; } } /* Print results */ System.out.println(smallest); System.out.println(largest); } }