jayati

Count Beautiful Substrings I

Nov 25th, 2023
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int beautifulSubstrings(string s, int k) {
  4.           int n = s.length();
  5.         std::unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
  6.         int count = 0;
  7.  
  8.         for (int i = 0; i < n; ++i) {
  9.             int vowelCount = 0, consonantCount = 0;
  10.  
  11.      
  12.             for (int j = i; j < n; ++j) {
  13.                 if (vowels.count(tolower(s[j])))
  14.                     vowelCount++;
  15.                 else
  16.                     consonantCount++;
  17.  
  18.  
  19.                 if (vowelCount == consonantCount && (vowelCount * consonantCount) % k == 0)
  20.                     count++;
  21.             }
  22.         }
  23.  
  24.         return count;
  25.        
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment