{"id":7475,"date":"2025-07-02T14:58:03","date_gmt":"2025-07-02T09:28:03","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7475"},"modified":"2025-07-02T14:58:03","modified_gmt":"2025-07-02T09:28:03","slug":"find-most-frequent-vowel-and-consonant","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/find-most-frequent-vowel-and-consonant\/","title":{"rendered":"Find Most Frequent Vowel and Consonant"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link\n  href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\"\n  rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n\n  .wp_blog_explanation h1,\n  .wp_blog_explanation h2,\n  .wp_blog_explanation h3,\n  .wp_blog_explanation h4,\n  .wp_blog_explanation h5,\n  .wp_blog_explanation h6,\n  .wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n  }\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n    <p>\n      This problem requires finding the most frequently occurring vowel and consonant characters in a string, and returning the sum of their frequencies.\n    <\/p>\n  \n    <h2>Steps<\/h2>\n    <ul>\n      <li>Initialize a character frequency map using a loop.<\/li>\n      <li>Define a list of vowels: <code>['a', 'e', 'i', 'o', 'u']<\/code>.<\/li>\n      <li>Traverse the string and count how often each character appears.<\/li>\n      <li>Track the highest frequency vowel and the highest frequency consonant.<\/li>\n      <li>Return the sum of those two highest values.<\/li>\n    <\/ul>\n  \n    <h2>Dry Run<\/h2>\n    <p><strong>Input:<\/strong> <code>s = \"successes\"<\/code><\/p>\n    <ol>\n      <li>Frequency map: { s: 4, u: 1, c: 2, e: 2 }<\/li>\n      <li>Vowels: u(1), e(2) \u2192 <code>maxVowels = 2<\/code><\/li>\n      <li>Consonants: s(4), c(2) \u2192 <code>maxConsonant = 4<\/code><\/li>\n      <li><strong>Output:<\/strong> <code>2 + 4 = 6<\/code><\/li>\n    <\/ol>\n  \n    <h2>Time &#038; Space Complexity<\/h2>\n    <ul>\n      <li><strong>Time Complexity:<\/strong> O(n), where n = length of the string<\/li>\n      <li><strong>Space Complexity:<\/strong> O(k), where k = number of unique characters (at most 26 lowercase letters)<\/li>\n    <\/ul>\n  <\/div>\n  \n\n  <div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <\/div>\n  \n    <!-- JavaScript -->\n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\nvar maxFreqSum = function (s) {\n    let map = {}\n    for (i = 0; i < s.length; i++) {\n        if (!map[s[i]]) {\n            map[s[i]] = 1\n        } else {\n            ++map[s[i]]\n        }\n    }\n\n    let vowels = ['a', 'e', 'i', 'o', 'u']\n    let maxVowels = 0\n    let maxConsonant = 0\n    for (let i = 0; i < s.length; i++) {\n        if (vowels.includes(s[i])) {\n            if (map[s[i]] > maxVowels) {\n                maxVowels = map[s[i]]\n            }\n        }\n        else {\n            if (map[s[i]] > maxConsonant) {\n                maxConsonant = map[s[i]]\n            }\n        }\n    }\n    return maxConsonant + maxVowels\n};        \n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- C++ -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n  #include &lt;iostream&gt;\n  #include &lt;string&gt;\n  #include &lt;unordered_map&gt;\n  using namespace std;\n  \n  int maxFreqSum(string s) {\n      unordered_map &lt;char,int&gt; freq;\n      for (char c : s) freq[c]++;\n  \n      string vowels = \"aeiou\";\n      int maxV = 0, maxC = 0;\n  \n      for (auto &p : freq) {\n          char ch = p.first;\n          int cnt = p.second;\n          if (vowels.find(ch) != string::npos) {\n              maxV = max(maxV, cnt);\n          } else {\n              maxC = max(maxC, cnt);\n          }\n      }\n  \n      return maxV + maxC;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- C -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n  #include &lt;stdio.h&gt;\n  #include &lt;string.h&gt;\n  #include &lt;ctype.h&gt;\n  \n  int maxFreqSum(char* s) {\n      int freq[256] = {0};\n      for (int i = 0; s[i]; i++) freq[(unsigned char)s[i]]++;\n  \n      char* vowels = \"aeiou\";\n      int maxV = 0, maxC = 0;\n  \n      for (int i = 0; s[i]; i++) {\n          unsigned char ch = s[i];\n          if (strchr(vowels, ch)) {\n              if (freq[ch] > maxV) maxV = freq[ch];\n          } else if (isalpha(ch)) {\n              if (freq[ch] > maxC) maxC = freq[ch];\n          }\n      }\n  \n      return maxV + maxC;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- Java -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\n  import java.util.*;\n  \n  public class Solution {\n      public int maxFreqSum(String s) {\n          Map&lt;Character,Integer&gt; freq = new HashMap<>();\n          for (char c : s.toCharArray())\n              freq.put(c, freq.getOrDefault(c,0) + 1);\n  \n          Set&lt;Character&gt; vowels = Set.of('a','e','i','o','u');\n          int maxV = 0, maxC = 0;\n  \n          for (var entry : freq.entrySet()) {\n              char ch = entry.getKey();\n              int cnt = entry.getValue();\n              if (vowels.contains(ch)) {\n                  maxV = Math.max(maxV, cnt);\n              } else {\n                  maxC = Math.max(maxC, cnt);\n              }\n          }\n  \n          return maxV + maxC;\n      }\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- Python -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\n  def maxFreqSum(s):\n      from collections import Counter\n      freq = Counter(s)\n      vowels = set('aeiou')\n  \n      maxV = max((freq[ch] for ch in freq if ch in vowels), default=0)\n      maxC = max((freq[ch] for ch in freq if ch not in vowels), default=0)\n  \n      return maxV + maxC\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n  \n  \n\n<a href=\"https:\/\/leetcode.com\/problems\/two-sum\/description\/\" target=\"blank\"\n  >Solve this problem.<\/a\n>\n<script>\n  document.addEventListener(\"DOMContentLoaded\", function () {\n    const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n    const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n    buttons.forEach((button) => {\n      button.addEventListener(\"click\", () => {\n        const lang = button.getAttribute(\"data-lang\");\n\n        buttons.forEach((btn) => btn.classList.remove(\"active\"));\n        button.classList.add(\"active\");\n\n        contents.forEach((content) => {\n          content.classList.toggle(\n            \"active\",\n            content.getAttribute(\"data-lang\") === lang\n          );\n        });\n      });\n    });\n  });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>This problem requires finding the most frequently occurring vowel and consonant characters in a string, and returning the sum of their frequencies. Steps Initialize a character frequency map using a loop. Define a list of vowels: [&#8216;a&#8217;, &#8216;e&#8217;, &#8216;i&#8217;, &#8216;o&#8217;, &#8216;u&#8217;]. Traverse the string and count how often each character appears. Track the highest frequency<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-7475","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-uncategorized"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7475"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7475\/revisions"}],"predecessor-version":[{"id":7486,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7475\/revisions\/7486"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}