String Construction

Sort by

recency

|

979 Discussions

|

  • + 0 comments

    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

    int stringConstruction(string s) {
        map<char, int> mp;
        int r = 0;
        for(int i = 0; i < s.size(); i++) if(!mp[s[i]]){
            mp[s[i]] = 1; r++;
        }
        return r;
    }
    

    solution 2 : using set

    int stringConstruction(string s) {
        set<char> se(s.begin(), s.end());
        return se.size();
    }
    
  • + 0 comments
    def stringConstruction(s):
        return len(set(s))
    
  • + 0 comments

    https://github.com/Achintha444/problem-solving-hackerrank-js/blob/main/100-string-construction.js

    answer in javascript

  • + 0 comments

    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; }

  • + 0 comments

    C++ One Liner

    int stringConstruction(string s) {
        return set<char>(s.begin(), s.end()).size();
    }