category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

E. Polycarp and String Transformation, Medium, Observation question

Approach
Code sample ```cpp #include <bits/stdc++.h> using namespace std; string helper(string str, char ch) { str.erase(remove(str.begin(), str.end(), ch), str.end()); return str; } int main() { int test; cin >> test; while (test--) { string str; cin >> str; unordered_map<char, int> mp; string tempString = ""; vector hash(26, 0); for (int i = 0; i < str.length(); ++i) hash[str[i] - 'a']++; int countElements = 0; for (auto i : hash) if (i > 0) countElements++; int flag = 0; for (int i = str.length() - 1; i >= 0; i--) { if (!mp.count(str[i]) && countElements > 0) { if (hash[str[i] - 'a'] % countElements != 0) { flag = 1; break; } int k = hash[str[i] - 'a'] / countElements; mp[str[i]] = k; tempString += str[i]; countElements--; } } if (flag == 0) { reverse(tempString.begin(), tempString.end()); string ans = ""; for (int i = 0; i < str.length(); ++i) { if (mp[str[i]] == 0) break; ans += str[i]; mp[str[i]]--; } string temp = ans, temp2 = ans; for (int i = 0; i < tempString.length(); ++i) { // Use helper function to remove all occurance of a character from string!! temp2 = helper(temp2, tempString[i]); temp += temp2; } if (temp == temp2) cout << ans << " " << tempString << endl; else cout << -1 << endl; } else { cout << -1 << endl; } } } ``` </details>