Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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 )
  3.  
  4. Input format
  5. First line contains an integer T - Number of test cases.
  6.  
  7. 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.
  8.  
  9. The second line of each test case contains a string s1.
  10.  
  11. The third line of each test case contains a string s2.
  12.  
  13. Output format
  14. For each test case, print Yes if the given strings are Similar or No if the given strings are not similar.
  15.  
  16. Sample Input 1
  17. 2
  18.  
  19. 5 3 2
  20.  
  21. aaabc
  22.  
  23. abc
  24.  
  25. 12 3 3
  26.  
  27. xyzzzbbbbbxx
  28.  
  29. bxy
  30.  
  31. Sample Output 1
  32. Yes
  33.  
  34. No
  35.  
  36. Explanation
  37. 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.
  38.  
  39. 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.
  40.  
  41. Constraints
  42. 1 <= T <= 100
  43.  
  44. 1 <= N,M <= 100000
  45. */
  46. function getFrequency(str,len,map){
  47.   for(let i=0;i<len;i++){
  48.     let curr= str.charAt(i);
  49.     if(map.has(curr)){
  50.       map.set(curr,map.get(curr)+1);
  51.     }
  52.     else
  53.       map.set(curr,1);
  54.   }
  55.  
  56.   return map;
  57.  
  58. }
  59. /**
  60.  * @param {number} n
  61.  * @param {number} m
  62.  * @param {number} k
  63.  * @param {String} s1
  64.  * @param {String} s2
  65.  * @return {String}
  66.  */
  67. function similarString(n, s1, m, s2, k) {
  68.   let ans="Yes";
  69.   let freMap1=new Map();
  70.   let freMap2=new Map();
  71.   freMap1= getFrequency(s1,n,freMap1);
  72.   freMap2=getFrequency(s2,m,freMap2);
  73.   freMap1.forEach((value,key)=>{
  74.     //  console.log(`value=${value}, key=${key}`);
  75.     if(((!freMap2.has(key)&&value>k))||(freMap2.has(key)&&Math.abs(freMap2.get(key)-value)>k)){
  76.       ans='No';
  77.     }
  78.     return
  79.   })
  80.  
  81.   freMap2.forEach((value,key)=>{
  82.     //  console.log(`value=${value}, key=${key}`);
  83.     if(((!freMap1.has(key)&&value>k))||(freMap1.has(key)&&Math.abs(freMap1.get(key)-value)>k)){
  84.       ans='No';
  85.     }
  86.     return
  87.   })
  88.  
  89.   return ans;
  90.  
  91.  
  92.  
  93. }
  94.  
  95. function main() {
  96.   let t = readLine();
  97.  
  98.   while (t-- > 0) {
  99.     const [n, m, k] = readIntArr();
  100.     let s1 = readLine();
  101.     let s2 = readLine();
  102.  
  103.     const res = similarString(n, s1, m, s2, k);
  104.     console.log(res);
  105.   }
  106. }
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement