Java Substring Comparisons Hackerrank Solution
For Explanation Watch Video:
Sample Input 0
welcometojava
3
Sample Output 0
ava
wel
Code:
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) { String smallest = ""; String largest = ""; smallest = s.substring(0,k); largest = s.substring(0,k); for(int i=0;i<=s.length()-k;i++){ String sub = s.substring(i,i+k); if(smallest.compareTo(sub)>0){ smallest = sub; } if(largest.compareTo(sub)<0){ largest = sub; } } 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)); }}
Comments
Post a Comment