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 String Construction 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 – String Construction – Hacker Rank Solution
String Construction– Hacker Rank Solution
Problem:
Amanda has a string of lowercase letters that she wants to copy to a new string. She can perform the following operations with the given costs. She can perform them any number of times to construct a new string :
- Append a character to the end of string at a cost of dollar.
- Choose any substring of and append it to the end of at no charge.
Given strings , find and print the minimum cost of copying each to on a new line.
For example, given a string , it can be copied for dollars. Start by copying , and individually at a cost of dollar per character. String at this time. Copy to the end of at no cost to complete the copy.
Function Description
Complete the stringConstruction function in the editor below. It should return the minimum cost of copying a string.
stringConstruction has the following parameter(s):
- s: a string
Input Format
The first line contains a single integer , the number of strings.
Each of the next lines contains a single string, .
Constraints
Subtasks
- for of the maximum score.
Output Format
For each string print the minimum cost of constructing a new string on a new line.
Sample Input
2 abcd abab
Sample Output
4 2
Explanation
Query 0: We start with and .
- Append character ” to at a cost of dollar, .
- Append character ” to at a cost of dollar, .
- Append character ” to at a cost of dollar, .
- Append character ” to at a cost of dollar, .
Because the total cost of all operations is dollars, we print on a new line.
Query 1: We start with and .
- Append character ” to at a cost of dollar, .
- Append character ” to at a cost of dollar, .
- Append substring to at no cost, .
Because the total cost of all operations is dollars, we print on a new line.
Note
A substring of a string is another string that occurs “in” (Wikipedia). For example, the substrings of the string “” are “”, “” ,””, “”, “”, and “”.
String Construction – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class StringConstruction { static int stringConstruction(String s) { int count = 0; int charArray[] = new int[26]; for (int i = 0; i < s.length(); i++) { int index = s.charAt(i) - 'a'; charArray[index]++; } for (int i = 0; i < 26; i++) { if (charArray[i] != 0) count++; } return count; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); for (int a0 = 0; a0 < q; a0++) { String s = in.next(); int result = stringConstruction(s); System.out.println(result); } in.close(); } }
buy cialis 5mg for sale order cialis 10mg without prescription ed pills
order vermox 100mg for sale brand tadalis buy tadalis 20mg generic
buy indomethacin tablets order terbinafine 250mg pills buy suprax 100mg
buy amoxicillin no prescription buy arimidex 1 mg without prescription order biaxin generic
buy catapres online cheap catapres over the counter tiotropium bromide 9 mcg cost
minocycline order brand actos 30mg pioglitazone where to buy
brand accutane purchase zithromax pills zithromax 500mg ca
order leflunomide 10mg without prescription order leflunomide 20mg sale buy azulfidine sale
tadalafil 40mg sale buy tadalafil pill tadalafil 10mg canada
buy azipro online order omnacortil 5mg order neurontin 100mg online
buy oral stromectol deltasone 40mg cheap deltasone 5mg cost
furosemide tablet ventolin cost ventolin 4mg drug
buy generic levitra 20mg cheap vardenafil 20mg buy generic hydroxychloroquine
ramipril 5mg price altace 5mg brand etoricoxib 120mg cheap
buy generic vardenafil oral tizanidine hydroxychloroquine 400mg usa