Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are given two strings and an integer k,find whether the given two strings are similar or not. Two given strings s1 and s2 are similar if for each character the difference between the frequency of that character in s1 and s2 is at most k. If the given strings are similar then print Yes otherwise print No. (Note : Both strings s1 and s2 are in lowercase )
- Input format
- First line contains an integer T - Number of test cases.
- The first line of each test case contains three integers N, M and K where N is the length of the first string, M is the length of the second string.
- The second line of each test case contains a string s1.
- The third line of each test case contains a string s2.
- Output format
- For each test case, print Yes if the given strings are Similar or No if the given strings are not similar.
- Sample Input 1
- 2
- 5 3 2
- aaabc
- abc
- 12 3 3
- xyzzzbbbbbxx
- bxy
- Sample Output 1
- Yes
- No
- Explanation
- In the first test case, the difference between each and every characters frequency in both the strings is at most 2. Hence, they are similar strings.
- In the second test case, the difference between the frequency of letter b in first string and the frequency of letter b in second string is 4. Hence, they are not similar strings.
- Constraints
- 1 <= T <= 100
- 1 <= N,M <= 100000
- */
- function getFrequency(str,len,map){
- for(let i=0;i<len;i++){
- let curr= str.charAt(i);
- if(map.has(curr)){
- map.set(curr,map.get(curr)+1);
- }
- else
- map.set(curr,1);
- }
- return map;
- }
- /**
- * @param {number} n
- * @param {number} m
- * @param {number} k
- * @param {String} s1
- * @param {String} s2
- * @return {String}
- */
- function similarString(n, s1, m, s2, k) {
- let ans="Yes";
- let freMap1=new Map();
- let freMap2=new Map();
- freMap1= getFrequency(s1,n,freMap1);
- freMap2=getFrequency(s2,m,freMap2);
- freMap1.forEach((value,key)=>{
- // console.log(`value=${value}, key=${key}`);
- if(((!freMap2.has(key)&&value>k))||(freMap2.has(key)&&Math.abs(freMap2.get(key)-value)>k)){
- ans='No';
- }
- return
- })
- freMap2.forEach((value,key)=>{
- // console.log(`value=${value}, key=${key}`);
- if(((!freMap1.has(key)&&value>k))||(freMap1.has(key)&&Math.abs(freMap1.get(key)-value)>k)){
- ans='No';
- }
- return
- })
- return ans;
- }
- function main() {
- let t = readLine();
- while (t-- > 0) {
- const [n, m, k] = readIntArr();
- let s1 = readLine();
- let s2 = readLine();
- const res = similarString(n, s1, m, s2, k);
- console.log(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement