RainX_69

count minimum number of possible anagrams after atmost k change (IMPORTANT OA)

Jan 22nd, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/contest/gfg-weekly-coding-contest-86/problems/#
  2.  
  3. Given a string s of length n, maximize the number of possible *anagrams by doing atmost k special operations.
  4. A special operation is defined as replacing any character with any of the available 26 lower case alphabets. After doing atmost k operations return the total number of anagrams.
  5. Since the answer can be very large, return the answer modulo 10^9+7.
  6. *anagrams :- An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, the word "listen" can be rearranged into "silent".
  7.  
  8. Input : N=3  K=1  s="aac"
  9. Output : 6
  10. Explaination :
  11. After applying one operation we can "a" on first index to "b" and
  12. 6 possible anagrams are "abc","acb","bca","bac","cab","cba"
  13.  
  14. Input : N=3   K=2   s="abc"
  15. Output: 6
  16. Explaination :
  17. The string "abc" itself has the maximum possible anagrams.
  18.  
  19. Your Task:
  20. Your task is to count the number of possible anagrams modulo 10^9 + 7 .
  21.  
  22. Constraints:
  23. 1 <= N <= 10 ^ 5
  24. 1 <= K <= 10 ^ 5
  25.  
  26. --------------------------------------------------------------------------------------------------------------------------------------
  27.  
  28.     int mod=1000000007;
  29.     long long int binaryExpo(long long int x, long long int power){
  30.         long long int res=1;
  31.         while(power>0){
  32.             if(power%2==0){
  33.                 x=(x*x)%mod;
  34.                 power/=2;
  35.             }
  36.             else{
  37.                 res=(res*x)%mod;
  38.                 power=power-1;
  39.             }
  40.         }
  41.         return res;
  42.     }
  43.    
  44.     int fact(long long int n){
  45.         long long int res=1;
  46.         for(int i=1;i<=n;i++){
  47.             res=(res*i)%mod;
  48.         }
  49.         return res%mod;
  50.     }
  51.    
  52.     int maximumPossible(int n,int k,string s){
  53.         int freq[26]={0};
  54.         for(auto c: s){
  55.             freq[c-'a']++;
  56.         }
  57.        
  58.         multiset<pair<int,char>> mpp; // you can get away using only freq, but using a char helps to visual.
  59.        
  60.         for(int i=0;i<26;i++){
  61.             mpp.insert({freq[i],i+'a'});
  62.         }
  63.        
  64.         k=min(k,n);  // the maximum change you can do is just n only at max
  65.        
  66.         while(k>0 && abs(mpp.begin()->first-mpp.rbegin()->first)>=2){
  67.             auto tp=*mpp.begin();
  68.             mpp.erase(mpp.begin());
  69.            
  70.             auto rp=*mpp.rbegin();
  71.             mpp.erase(--mpp.end());
  72.            
  73.             tp.first++;
  74.             mpp.insert(tp);
  75.            
  76.             rp.first--;
  77.             mpp.insert(rp);
  78.            
  79.             k--;
  80.         }
  81.        
  82.         int res=fact(n);
  83.        
  84.         for(auto m: mpp){
  85.           int fx=fact(m.first);
  86.           res=(res*binaryExpo(fx,mod-2))%mod;  // fermat little theorem
  87.         }
  88.         return res;
  89.     }
  90.  
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment