0 like 0 dislike
1,321 views

All past online assesments of Google can be found using the tag "google_oa" in the search bar.

| 1,321 views

0 like 0 dislike

# Google Online Assessment (OA)  | Split Strings

Given a string `s` with length `n`, how many ways can you split it into two substrings `s_1` and `s_2` such that the number of unique characters in `s_1` and `s_2` are the same?

Parameter

• `s`: A string with length `n`.

Result

• The number of ways you can split it into two substrings that satisfy the condition.

### Examples

Example 1:

Input: `s = "aaa"`

Output: `2`

Explanation: It can be split in two ways: `"a", "aa"` and `"aa", "a"`.

Example 2:

Input: `s = "bac"`

Output: `0`

Explanation: There is no way to split this string into two substrings with equal unique elements.

### Constraints

• `0 <= n <= 10^5`
• Characters in this string may consist of any alphanumeric character. The characters are case sensitive. That is, same letters with different cases count as different characters.
by Expert (34,270 points)
0 0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace chrono;
using namespace __gnu_pbds;
#define pb push_back
#define inf (1LL << 60)
#define all(x) (x).begin(), (x).end()
#define prDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<int, pair<int, int>>
#define fast_io                       \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define int long long
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define max4(a, b, c, d) max(a, max3(b, c, d))
#define min4(a, b, c, d) min(a, min3(b, c, d))
#define max5(a, b, c, d, e) max(max4(a, b, c, d), e)
#define min5(a, b, c, d, e) min(min4(a, b, c, d), e)
#define MAXN 100005
#define INF 1e18
#define mod 1000000007
#define mod2 998244353
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // find_by_order, order_of_key
const int N = 1e5 + 10;
int query(int l, int r)
{
cout << "? " << l << " " << r << endl;
int ans;
cin >> ans;
return ans;
}
int32_t main()
{
fast_io;
int tc = 1;
cin >> tc;
while (tc--)
{
string s;
cin >> s;
int n = s.size();
// no of ways to split the array !!
map<int, int> mp;
int st[n];
int ed[n];
for (int i = 0; i < n; i++)
{
mp[s[i]]++;
st[i] = mp.size();
}
mp.clear();
for (int i = n - 1; i >= 0; i--)
{
mp[s[i]]++;
ed[i] = mp.size();
}
int ans = 0;
for (int i = 0; i < n; i++)
{
if (st[i] == ed[i + 1])
{
ans++;
}
}
cout << ans << endl;
}

return 0;
}