String Construction

Sort by

recency

|

985 Discussions

|

  • + 0 comments

    Approach:

    1. Use set to strore the element.
    2. Traverse the string and inset all the string character inside the set.
    3. As it is set, It stores only distinct element
    4. Return the size of the set.
  • + 0 comments

    I'm quite suprised how easy this problem is given that it's worth 25 points. Here's my Python solution!

    def stringConstruction(s):
        return len(set(list(s)))
    
  • + 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(list(set(s)))
    

    or

    def stringConstruction(s):
        bitmask = 0
        for char in s:
            bitmask |= (1 << (ord(char) - ord('a')))
        return bin(bitmask).count('1')
    
  • + 0 comments

    My answer with Typescript,

    function stringConstruction(s: string): number {
        // count number of unique character in [s]
        return new Set(s.split('')).size
    }