String Construction

Sort by

recency

|

977 Discussions

|

  • + 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();
    }
    
  • + 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

    Python3

    def stringConstruction(s):

    return sum([1 for _ in set(s)])
    
  • + 0 comments

    Java

    public static int stringConstruction(String s) {
            Set<Character> uniqueElements = new HashSet<>();
            for (char character : s.toCharArray()) {
                uniqueElements.add(character);
            }
            return uniqueElements.size();
        }