Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in 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 HackerRank in a String! in Java-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 Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – HackerRank in a String! – Hacker Rank Solution
HackerRank in a String! – Hacker Rank Solution
Problem:
We say that a string contains the word hackerrank
if a subsequence of its characters spell the word hackerrank
. Remeber that a subsequence maintains the order of characters selected from a sequence.
More formally, let be the respective indices of h
, a
, c
, k
, e
, r
, r
, a
, n
, k
in string . If is true, then contains hackerrank
.
For each query, print YES
on a new line if the string contains hackerrank
, otherwise, print NO
.
Example
This contains a subsequence of all of the characters in the proper order. Answer YES
This is missing the second ‘r’. Answer NO
.
There is no ‘c’ after the first occurrence of an ‘a’, so answer NO
.
Function Description
Complete the hackerrankInString function in the editor below.
hackerrankInString has the following parameter(s):
- string s: a string
Returns
- string:
YES
orNO
Input Format
The first line contains an integer , the number of queries.
Each of the next lines contains a single query string .
Constraints
Sample Input 0
2 hereiamstackerrank hackerworld
Sample Output 0
YES NO
Explanation 0
We perform the following queries:
The characters ofhackerrank
are bolded in the string above. Because the string contains all the characters inhackerrank
in the same exact order as they appear inhackerrank
, we returnYES
.- does not contain the last three characters of
hackerrank
, so we returnNO
.
Sample Input 1
2 hhaacckkekraraannk rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt
Sample Output 1
YES NO
HackerRank in a String! – Hacker Rank Solution
import java.util.Scanner; public class LCS { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int q = scan.nextInt(); while (q-- > 0) { String s1 = scan.next(); String s2 = "hackerrank"; int m = s1.length(); int n = s2.length(); char a[] = s1.toCharArray(); char b[] = s2.toCharArray(); int c[][] = new int[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (b[i - 1] == a[j - 1]) { c[i][j] = c[i - 1][j - 1] + 1; } else { c[i][j] = Math.max(c[i - 1][j], c[i][j - 1]); } } } int count = c[n][m]; if(count == n) { System.out.println("YES"); } else { System.out.println("NO"); } } } }