-2

#include<bits/stdc++.h>
using namespace std;
void solve()
{
    int n,cnt;
    cin >> n;
    string s,root="";
    vector<string>x;
    for(int i=0; i<n; i++)
    {
        cin >> s;
        sort(s.begin(),s.end());
        for(int i=0; i<s.size(); i++)
        {
            if(s[i] != s[i+1])
            {
                root+=s[i];
            }
        }
        x.push_back(root);
        root="";
    }
    cnt=0;
    for(int j=1; j<x.size(); j++)
    {
        if(x[j]!=(x[j-1]))
        {
           cnt++;
        }

        //cout << x[j] <<" ";
    }
     cout << cnt+1;
    
   
}
int main()
{
    solve();
}


`here i wanted to compare the strings as they were same or not. i wanted to print how many distinct strings there are

i tried to store the strings in a array of strings.help me solve this problem.`

1 Answers1

0

this can be accepted

#include<iostream>
#include<set>
#include<string>

void solve()
{
    int n,cnt;
    std::cin >> n;
    std::set<std::set<char>> st;
    std::string s;
    for(int i=0; i<n; i++)
    {
        std::cin >> s;
        std::set<char> t{s.begin(),s.end()};
        st.insert(t);
    }
    std::cout << st.size();
}
int main()
{
    solve();
}

use set to store unique letter of the string as a root, then use another set to store different root

ads169573
  • 1
  • 1