byte[] explained || 2 ways || fastest and simplest || best for beginners
\n# jsut read and you will understand the approach : \nlet word = "abei"\nAll possible substrings:\n\n\na        b       e      i\nab       be      ei\nabe     
\n# jsut read and you will understand the approach : \nlet word = "abei"\nAll possible substrings:\n```\n\na        b       e      i\nab       be      ei\nabe      bei\nabei\n```\nSo for counting occurences of vowels in each substring, we can count the occurence of each vowel in each substring\nIn this example, count of vowels in substrings are\n```\na - 4 times\ne - 6 times\ni - 4 times\n```\nAnd you can observe that occurence of each vowel depends on it\'s position like below:\n\na is at position 0 so it can be present only in the substrings starting at 0\ne is in the middle so it can be present in the substrings starting at it\'s position, substrings ending at it\'s position, and substrings containing it in middle\ni is at last so it can be present only in the substrings ending at that position\nDid you see any pattern ? Yes !\n\nA character at position pos can be present in substrings starting at i and substrings ending at j,\nwhere 0 <= i <= pos (pos + 1 choices) and pos <= j <= len (len - pos choices)\nso there are total (len - pos) * (pos + 1) substrings that contain the character word[pos]\n\n(You can see from above example that e is at position 2 and it\'s present in substrings "abei", "bei", "ei", "abe", "be", "e"\n(0 <= start <= pos and pos <= end <= len) and same will be true for each vowel.\n\n\nFrom this observation we can generalise the occurence of each vowel in the string as\n\nhere len(abei) = 4\n```\na,  pos = 0, count = (4 - 0) * (0 + 1) = 4\ne,  pos = 2, count = (4 - 2) * (2 + 1) = 6\ni,  pos = 3, count = (4 - 3) * (3 + 1) = 4\n```\nSo the generalised formula will be\n\ncount = (len - pos) * (pos + 1)\nand we can keep summing the count of each vowel (don\'t forget to convert to long)\n## faster code is just below \n# Code\n```\nclass Solution {\n    public boolean isVowel(char ch){\n        return ch==\'a\'||ch==\'e\'||ch==\'i\'||ch==\'o\'||ch==\'u\';\n    }\n    public long countVowels(String word) {\n        long count = 0 ; \n        int len = word.length() ; \n        for(int i=0;i<len;i++){\n            if(isVowel(word.charAt(i))){\n                count+=(long)(len-i)*(long)(i+1);\n            }\n        }\n        return count ; \n    }\n}\n```\n \n![image.png](https://assets.leetcode.com/users/images/3680f2d2-ba7c-45a8-9b08-ece850213b64_1696235645.8015516.png)\nexplanation is just below \n\n```\nclass Solution {\n    public long countVowels(String word) {\n        long n = word.length();\n        long res = 0; int i = 0;\n        byte[] w = word.getBytes();\n        for (byte c : w) {\n            c = w[i];\n            if (c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\') {\n                res += (n - i) * (i + 1);\n            }\n            i++;\n        }\n        return res;\n    }\n}\n```\n\n\n\n## Explanation:\n\n- The function countVowels takes a string word as input and calculates the count of vowels in the word using a specific formula.\n\n- An array of byte called vowels is created with a size of 128. Each element of this array corresponds to a character\'s ASCII value, and if the character is a vowel (\'a\', \'e\', \'i\', \'o\', \'u\'), the corresponding element in the vowels array is set to 1.\n\n- res is initialized to 0, and prev is unused in this code.\n\n- byte[] arr is created by encoding the input word into bytes using the default character encoding. This allows the code to work with any string.\n\n- The code then iterates over each character in the arr array. If the character is a vowel (as determined by looking up the vowels array), it calculates a value and adds it to res.\n\n- The value added to res is (i + 1) * (len - i), which appears to be a way of counting the position of the vowel within the word and how many characters are to the right of it.\n\n- The final result, stored in res, represents the count of vowels according to the formula used.\n\n\n\n# some more info \nThe `getBytes()` function does convert a string into an array of bytes, but the resulting `byte[]` does not directly contain the ASCII values of the characters. Instead, it contains the bytes representing the characters based on the character encoding used.\n\nIf you have the string "abc" and you call `getBytes()` without specifying a character encoding, it will use the platform\'s default encoding (usually UTF-8 on modern systems). In this case, the resulting `byte[]` will contain the UTF-8 encoded bytes for the characters \'a\', \'b\', and \'c\'.\n\nSo, the `byte[]` array might look something like this (assuming UTF-8 encoding):\n\n```\n[97, 98, 99]\n\n```\n\nIn this example, `97` corresponds to the UTF-8 representation of \'a\', `98` corresponds to \'b\', and `99` corresponds to \'c\'. These values are not the ASCII values but rather the UTF-8 encoded bytes for these characters. If you want to obtain the ASCII values, you can simply cast them to `int`:\n\n```\nint asciiValueOfA = (int) w[0]; // This will be 97\nint asciiValueOfB = (int) w[1]; // This will be 98\nint asciiValueOfC = (int) w[2]; // This will be 99\n\n```\n\nKeep in mind that if you specify a different character encoding when calling `getBytes()`, the resulting `byte[]` will contain bytes according to that encoding, which may not be the same as the ASCII values.
['Java']