Digit Operation Solution
Problem
You are given arrays A and B, each consisting of N strings, and a positive integer K.
For all 1≤i≤N, both Ai and Bi consist of characters from 0 to 9 (both inclusive).
You can perform the following types of operations on array A:
- Type 1: Choose two indices i and j (i=j) and swap any character of Ai with any character of Aj. The cost of this operation is 0.
- Type 2: Choose an index i and replace one character of Ai with any character from 0 to 9 (both inclusive). The cost of this operation is 1.
For example, let A=[24,30,51]. A valid sequence of operations can be:
- Swap 4 in A1=24 with 0 in A2=30 to obtain [20,34,51].
- Swap 0 in A1=20 with 5 in A3=51 to obtain [25,34,01].
- Replace 0 in A3=01 with 1 to obtain [25,34,11].
The cost of the above sequence of operations is 1.
Find whether we can convert the array A to array B using any (possibly zero) number of operations with cost ≤K.
Input Format
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and K, the size of array and the maximum cost of operations.
- The second line of each test case contains N space-separated strings A1,A2,…,AN.
- The third line of each test case contains N space-separated strings B1,B2,…,BN.
Output Format
For each test case, print YES
if it is possible to convert array A to B using any (possibly zero) number of operations with cost ≤K. Else, print NO
.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs
, yes
, Yes
, and YES
will all be treated as identical).
Constraints
- 1≤T≤1000
- 2≤N≤105
- 1≤K≤1018
- 1≤∣Ai∣,∣Bi∣≤19
- Ai and Bi consist of characters from 0 to 9 (both inclusive).
- The sum of N over all test cases won't exceed 105.
Sample 1:
Explanation:
Test case 1: We can use one operation of type 1 to swap 1 in A1 with 9 in A2. Thus, A becomes [9,1], which is equal to B. We are able to do this with 0 cost, which is ≤2.
Test case 2: It is impossible to convert A to B with at most 2 cost.
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define F first
#define S second
#define pb push_back
#define si set <int>
#define vi vector <int>
#define pii pair <int, int>
#define vpi vector <pii>
#define vpp vector <pair<int, pii>>
#define mii map <int, int>
#define mpi map <pii, int>
#define spi set <pii>
#define endl "\n"
#define sz(x) ((int) x.size())
#define all(p) p.begin(), p.end()
#define double long double
#define que_max priority_queue <int>
#define que_min priority_queue <int, vi, greater<int>>
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a) for(auto x : a) cout << x << " "; cout << endl
#define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
#define scanit(a,n) for(ll indexaa=0; indexaa<n; indexaa++) cin>>a[indexaa];
inline int power(int a, int b)
{
int x = 1;
while (b)
{
if (b & 1) x *= a;
a *= a;
b >>= 1;
}
return x;
}
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
const char* comma = strchr (names + 1, ',');
cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
const int N = 200005;
void solve() {
int n,k;
cin>>n>>k;
vector<string>v(n),b(n);
for (int i = 0; i < n; i++)
{
cin>>v[i];
}
for (int i = 0; i < n; i++)
{
cin>>b[i];
}
map<char,map<char,int>>m;
for (int i = 0; i < n; i++)
{
if(v[i].length()!=b[i].length()){
cout<<"NO\n";
return;
}
for (int j = 0; j < v[i].size(); j++)
{
if(v[i][j]!=b[i][j])m[v[i][j]][b[i][j]]++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < v[i].size(); j++)
{
if(v[i][j]!=b[i][j]){
if(m[b[i][j]][v[i][j]]>0){
m[b[i][j]][v[i][j]]--;
}
else k--;
}
}
}
if(k<0)cout<<"NO\n";
else cout<<"YES\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t z = clock();
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.