Interview Question - String Deduplication

For the problem of deduplication of strings, the time and space complexity are required to be very small. I have been thinking about it for a long time, and I don't know if using set will be relatively smaller. I hope the big cows can correct me.

Next

Both set and multiset are implemented based on red-black trees, in which search, deletion and insertion operations all take O(logk) time.
Header files required for set and multiset:
#include

definition:

set/multiset <type> name.
The difference between set and multiset: A
set does not have the same elements, while a multiset can have the same elements.

The same operation of set and multiset:

s.insert(x) inserts the x element into s.
s.empty()=0 s have elements, s.empty()=1 s have no elements.
multiset/set<type>::iterator it=s.lower_bound(x) Returns the minimum value of s greater than or equal to x, and the position of this value in s is it, and *it represents the value of this position.
multiset/set<type>::iterator it=s.upper_pound(x) Returns the smallest value in s greater than x, and the position of this value in s is it, and *it represents the value of this position.

Different operations of set and multiset:

When s is set, s.erase(x) deletes the x element in s.
When s is multiset, s.erase(it) deletes the number at the it position in s

Because I saw it on the Swordsman offer

typedef multiset < int, greater< int > > intSet;

So I checked the syntax description of typedef

Syntax description of typedef

In order to solve the requirement of user-defined data type names, the type redefinition statement typedef is introduced in C language, which can define new type names for data types, thereby enriching the attribute information contained in data types.

Syntax description of typedef

typedef type name type identifier;

比如 typedef multiset < int, greater< int > > intSet;
intSet set;

Then the type of set at this time is multiset < int, greater < int >> >

Below is my implementation of string deduplication

#include<iostream>
#include<set>
#include<string>
using namespace std;

int main(){
    typedef set<char> set_t;
    set_t str;
    string str1;
    cin >> str1;
    for (int i = 0; i < str1.size(); i++){
        str.insert(str1[i]);
    }
    //str.cbegin()也就是只允许读,不允许改
    //str.cedn()也是只允许读,不允许改
    for (set_t::const_iterator p = str.cbegin(); p != str.cend(); ++p)
        cout << *p ;
    system("pause");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325502887&siteId=291194637