RainX_69

Count substrings having atmost one letter odd number of times | HARD | OA | MUST DO

Apr 8th, 2023 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | Source Code | 0 0
  1. https://leetcode.com/problems/number-of-wonderful-substrings/
  2.  
  3. A wonderful string is a string where at most one letter appears an odd number of times.
  4. For example, "ccjjc" and "abab" are wonderful, but "ab" is not.
  5. Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.
  6. A substring is a contiguous sequence of characters in a string.
  7.  
  8.  
  9.  
  10. Example 1:
  11. Input: word = "aba"
  12. Output: 4
  13. Explanation: The four wonderful substrings are underlined below:
  14. - "aba" -> "a"
  15. - "aba" -> "b"
  16. - "aba" -> "a"
  17. - "aba" -> "aba"
  18.  
  19. Example 2:
  20. Input: word = "aabb"
  21. Output: 9
  22. Explanation: The nine wonderful substrings are underlined below:
  23. - "aabb" -> "a"
  24. - "aabb" -> "aa"
  25. - "aabb" -> "aab"
  26. - "aabb" -> "aabb"
  27. - "aabb" -> "a"
  28. - "aabb" -> "abb"
  29. - "aabb" -> "b"
  30. - "aabb" -> "bb"
  31. - "aabb" -> "b"
  32.  
  33. Example 3:
  34. Input: word = "he"
  35. Output: 2
  36. Explanation: The two wonderful substrings are underlined below:
  37. - "he" -> "h"
  38. - "he" -> "e"
  39.  
  40. Constraints:
  41. 1 <= word.length <= 10^9
  42. word consists of lowercase English letters from 'a' to 'j'.
  43.  
  44. -----------------------------------------------------------------------------------------------------------------------
  45.  
  46. class Solution {
  47. public:
  48.     long long helper(string &word, char x){
  49.         int desiredMask=(1 << (x-'a'));
  50.         int mask=0;
  51.         long long res=0;
  52.        
  53.         unordered_map<int,int> mpp;
  54.        
  55.         for(auto c: word){
  56.             mask^=(1 << (c-'a'));
  57.             if(mask==desiredMask){
  58.                 res++;
  59.             }
  60.             if(mpp.find(desiredMask ^ mask)!=mpp.end()){
  61.                 res+=mpp[desiredMask ^ mask];
  62.             }
  63.             mpp[mask]++;
  64.         }
  65.         return res;
  66.     }
  67.    
  68.     long long zeroXOR(string &word){
  69.         long long res=0;
  70.         int mask=0;
  71.  
  72.         unordered_map<int,int> mpp;
  73.        
  74.         for(auto x: word){
  75.             mask^=(1 << (x-'a'));
  76.             if(mask==0){
  77.                 res++;
  78.             }
  79.             if(mpp.find(mask)!=mpp.end()){
  80.                 res+=mpp[mask];
  81.             }
  82.             mpp[mask]++;
  83.         }
  84.         return res;
  85.     }
  86.    
  87.     long long wonderfulSubstrings(string word) {
  88.         long long res=0;
  89.         for(int i=0;i<10;i++){
  90.             res+=helper(word,i+'a');
  91.         }
  92.         res+=zeroXOR(word); // Because if there is no odd count of letters, XOR will be zero of the subaray
  93.         return res;
  94.     }
  95. };
Advertisement
Add Comment
Please, Sign In to add comment