Java Substring - Hacker Rank Solution Java

Java Substring - Hacker Rank Solution Java

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

Java Substring - Hacker Rank Solution Java


Table of Content (toc)

HackerRank Java Substring - Problem Statement

Given a string, s, and two indices, start and end, print a substring consisting of all characters in the inclusive range from string to end-1. You'll find the String class' substring method helpful in completing this challenge.

Input Format

The first line contains a single string denoting s.
The second line contains two space-separated integers denoting the respective values of start and end.

Constraints

  • 1 <= |s| <=100
  • 0 <= start < end <= n
  • String s consists of English alphabetic letters (i.e., [a-zA-z]) only.

Output Format

Print the substring in the inclusive range from start to end-1.

Sample Input

Helloworld 3 7 (code-box)

Sample Output

lowo (code-box)

Explanation

In the diagram below, the substring is highlighted in green:

substring

Java Substring - Hacker Rank Solution

Approach I:

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 scan = new Scanner(System.in);
        String str = scan.next();
        int start  = scan.nextInt();
        int end    = scan.nextInt();
        scan.close();
        
        System.out.println(str.substring(start, end));
    }
}


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

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

All the Best!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.