# String Construction in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

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.

• 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 ProblemString 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

•  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

1. Append character  to  at a cost of  dollar, .
2. Append character  to  at a cost of  dollar, .
3. Append character  to  at a cost of  dollar, .
4. Append character  to  at a cost of  dollar, .

Because the total cost of all operations is  dollars, we print  on a new line.

1. Append character  to  at a cost of  dollar, .
2. Append character  to  at a cost of  dollar, .
3. 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;
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();
}
}```  