Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java Substring Comparisons hackerrank solution

 We define the following terms:

  • For example, ball < catdog < dormHappy < happyZoo < ball.
  • substring of a string is a contiguous block of characters in the string. For example, the substrings of abc are abcabbc, 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 denotings s .
The second line contains an integer denoting K.

Constraints

  •  consists of English alphabetic letters only (i.e., [a-zA-Z]).

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 :

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.

solution:-




import java.util.Scanner;



public class Solution {



    public static String getSmallestAndLargest(String s, int k) {


       String smallest = "";


    String largest = "";


    smallest = largest = s.substring(0, k);



    for (int i=1; i<s.length()-k+1; i++) {


        String substr = s.substring(i, i+k);


        if (smallest.compareTo(substr) > 0)


            smallest = substr;


        if (largest.compareTo(substr) < 0)


            largest = substr;


    }



    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