Codeforces Round 859 (Div. 4) 1807C - Find and Replace Solution
Problem Link : https://codeforces.com/contest/1807/problem/C
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std ;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t ;
- cin >> t ;
- while ( t-- ){
- int n ;
- cin >> n ;
- string s ;
- cin >> s ;
- string s1, s2 ;
- for ( int i = 0 ; i < n ; i++) {
- if ( i % 2 == 0) {
- s1.push_back('0');
- s2.push_back('1');
- }
- else {
- s1.push_back('1');
- s2.push_back('0');
- }
- }
- map < char ,char > mp1 , mp2 ;
- bool x = true , y = true ;
- for ( int i = 0 ; i < n ; i++ ) {
- if ( mp1.find(s[i])!=mp1.end()){
- if(mp1[s[i]] != s1[i]) {
- x = false ;
- }
- }
- else
- mp1.insert({s[i],s1[i]}) ;
- }
- for ( int i = 0 ; i < n ; i++ ) {
- if ( mp2.find(s[i] )!=mp2.end()){
- if(mp2[s[i]] != s2[i]) {
- y = false ;
- }
- }
- else
- mp2.insert({s[i],s2[i]}) ;
- }
- if ( x == true || y == true ) {
- cout << "YES"<<endl;
- }
- else
- cout <<"NO"<<endl;
- }
- return 0 ;
- }
No comments