File tree Expand file tree Collapse file tree 5 files changed +134
-0
lines changed Expand file tree Collapse file tree 5 files changed +134
-0
lines changed Original file line number Diff line number Diff line change
1
+ vector<string> uncommonFromSentences (string A, string B) {
2
+ int len1 = A.size ();
3
+ int len2 = B.size ();
4
+ if (len1 == 0 && len2 == 0 )
5
+ return {};
6
+
7
+ map<string, int > mp;
8
+ string::size_type i = A.find_first_not_of (" " );
9
+ while (i != string::npos)
10
+ {
11
+ string::size_type pos = A.find (' ' , i);
12
+ if (pos != string::npos)
13
+ {
14
+ mp[A.substr (i, pos-i)]++;
15
+ i = A.find_first_not_of (" " , pos+1 );
16
+ }
17
+ else
18
+ {
19
+ mp[A.substr (i)]++;
20
+ break ;
21
+ }
22
+ }
23
+
24
+ i = B.find_first_not_of (" " );
25
+ while (i != string::npos)
26
+ {
27
+ string::size_type pos = B.find (' ' , i);
28
+ if (pos != string::npos)
29
+ {
30
+ mp[B.substr (i, pos-i)]++;
31
+ i = B.find_first_not_of (" " , pos+1 );
32
+ }
33
+ else
34
+ {
35
+ mp[B.substr (i)]++;
36
+ break ;
37
+ }
38
+ }
39
+
40
+ vector<string> res;
41
+ for (auto & m : mp)
42
+ {
43
+ if (m.second == 1 )
44
+ res.push_back (m.first );
45
+ }
46
+
47
+ return res;
48
+ }
Original file line number Diff line number Diff line change
1
+ int numSpecialEquivGroups (vector<string>& A) {
2
+ int len = A.size ();
3
+ vector<vector<int >> vec (len, vector<int >(52 , 0 ));
4
+ for (int i = 0 ; i < len; ++i)
5
+ {
6
+ int sz = A[i].size ();
7
+ for (int j = 0 ; j < sz; ++j)
8
+ {
9
+ ++vec[i][j%2 *26 +A[i][j]-' a' ];
10
+ }
11
+ }
12
+
13
+ auto cmp = [](const vector<int >& a, const vector<int >& b) {
14
+ for (int i = 0 ; i < 52 ; ++i)
15
+ {
16
+ if (a[i] == b[i])
17
+ continue ;
18
+ else if (a[i] < b[i])
19
+ return true ;
20
+ else
21
+ return false ;
22
+ }
23
+ return false ;
24
+ };
25
+
26
+ sort (vec.begin (), vec.end (), cmp);
27
+ return unique (vec.begin (), vec.end ()) - vec.begin ();
28
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > sortArrayByParity (vector<int >& A) {
2
+ int len = A.size ();
3
+ if (len <= 1 )
4
+ return A;
5
+ int i = 0 , j = len-1 ;
6
+ while (i < j)
7
+ {
8
+ while (i < j && A[i]%2 == 0 )
9
+ ++i;
10
+ while (i < j && A[j]%2 == 1 )
11
+ --j;
12
+ if (i < j)
13
+ swap (A[i], A[j]);
14
+ }
15
+
16
+ return A;
17
+ }
Original file line number Diff line number Diff line change
1
+ int smallestRangeI (vector<int >& A, int K) {
2
+ int len = A.size ();
3
+ if (len <= 1 )
4
+ return 0 ;
5
+
6
+ sort (A.begin (), A.end ());
7
+ int diff = A[len-1 ] - A[0 ];
8
+ return diff <= 2 *K ? 0 : diff-2 *K;
9
+ }
Original file line number Diff line number Diff line change
1
+ int numUniqueEmails (vector<string>& emails) {
2
+ int len = emails.size ();
3
+ if (len <= 1 )
4
+ return len;
5
+
6
+ set<string> uniqueEml;
7
+ for (int i = 0 ; i < len; ++i)
8
+ {
9
+ int sz = emails[i].size ();
10
+ string res (sz, ' ' );
11
+ int j = 0 , k = 0 ;
12
+ while (j < sz && emails[i][j] != ' @' )
13
+ {
14
+ if (emails[i][j] == ' +' )
15
+ break ;
16
+ else if (emails[i][j] != ' .' )
17
+ res[k++] = emails[i][j];
18
+ ++j;
19
+ }
20
+ while (emails[i][j] != ' @' )
21
+ ++j;
22
+ while (j < sz)
23
+ {
24
+ res[k] = emails[i][j];
25
+ ++j;
26
+ ++k;
27
+ }
28
+ uniqueEml.insert (res.substr (0 , k));
29
+ }
30
+
31
+ return uniqueEml.size ();
32
+ }
You can’t perform that action at this time.
0 commit comments