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 Two Characters 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 – Two Characters– Hacker Rank Solution
Two Characters – Hacker Rank Solution
Problem:
Given a string, remove characters until the string is made up of any two alternating characters. When you choose a character to remove, all instances of that character must be removed. Determine the longest string possible that contains just two alternating letters.
Example
Delete a
, to leave bcdbd
. Now, remove the character c
to leave the valid string bdbd
with a length of 4. Removing either b
or d
at any point would not result in a valid string. Return .
Given a string , convert it to the longest possible string made up only of alternating characters. Return the length of string . If no string can be formed, return .
Function Description
Complete the alternate function in the editor below.
alternate has the following parameter(s):
- string s: a string
Returns.
- int: the length of the longest valid string, or if there are none
Input Format
The first line contains a single integer that denotes the length of .
The second line contains string .
Constraints
![Two Characters in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 86](https://technorj.com/wp-content/uploads/2021/12/image-86.png)
Sample Input
STDIN Function ----- -------- 10 length of s = 10 beabeefeab s = 'beabeefeab'
Sample Output
5
Explanation
The characters present in are a
, b
, e
, and f
. This means that must consist of two of those characters and we must delete two others. Our choices for characters to leave are [a,b], [a,e], [a, f], [b, e], [b, f] and [e, f].
If we delete e
and f
, the resulting string is babab
. This is a valid as there are only two distinct characters (a
and b
), and they are alternating within the string.
If we delete a
and f
, the resulting string is bebeeeb
. This is not a valid string because there are consecutive e
‘s present. Removing them would leave consecutive b's
, so this fails to produce a valid string .
Two Characters – Hacker Rank Solution
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 in=new Scanner(System.in); int n=in.nextInt(); String s=in.next(); String res=""; for(int i=0;i<26;i++){ for(int j=i+1;j<26;j++){ char a=(char)('a'+i); char b=(char)('a'+j); String cur=""; for(int k=0;k<n;k++){ if (s.charAt(k)==a || s.charAt(k)==b) { cur+=s.charAt(k); } } if (cur.length()<res.length()) continue; if (isGood(cur)) res=cur; } } System.out.println(res.length()); } public static boolean isGood(String s){ if (s.length()==1) return false; for(int i=1;i<s.length();i++){ if (s.charAt(i)==s.charAt(i-1)) return false; } return true; } }