We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- String Construction
- Discussions
String Construction
String Construction
Sort by
recency
|
979 Discussions
|
Please Login in order to post a comment
The solution here is to count the number of unique occurrences of the characters, here is the explanation : https://youtu.be/UqqNcIUnsv8
solution 1 : using map
solution 2 : using set
https://github.com/Achintha444/problem-solving-hackerrank-js/blob/main/100-string-construction.js
answer in javascript
include
using namespace std; int test(string s) { map mp; int sum = 0; for(int i = 0; i < s.length(); i++) { mp[s[i]]++; } for(auto it : mp) { if(it.second = 1) { sum += 1; } else if(it.second >= 2) { sum += 2; } } return sum; } int main() { int t; cin >> t; while(t--) { string s; cin >> s; cout << test(s) << endl; } return 0; }
C++ One Liner