forked from aditiraj/hackerrankSolutions-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringConstruction.js
More file actions
25 lines (22 loc) · 752 Bytes
/
stringConstruction.js
File metadata and controls
25 lines (22 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function stringConstruction(s) {
/* Add each of the unique characters of the string to the set and increament the cost(case:copying character from old to new
string). If the set already has the character do not increament the cost(case:copying the substring from the new string at
no cost)*/
var mySet= new Set();
var cost=0;
for(let i=0; i<s.length; i++){
if(!mySet.has(s.charAt(i))){
mySet.add(s.charAt(i));
cost++;
}
}
return cost;
}
function main() {
var q = parseInt(readLine());
for(var a0 = 0; a0 < q; a0++){
var s = readLine();
var result = stringConstruction(s);
process.stdout.write("" + result + "\n");
}
}