id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "bool sortCondition(pair<int,int> &pq1,pair<int,int> &pq2)\n{\n return pq1.second>pq2.second;\n}\nclass Solution {\npublic:\n int minimumPushes(string &word) {\n int ans=0;\n vector<pair<int,int>> pq;\n for(int i=0;i<26;i++)\n {\n pq.push_back({i,0});\n }\n for(auto chr:word)\n {\n pq[chr-'a']={chr-'a',pq[chr-'a'].second+1};\n }\n sort(pq.begin(),pq.end(),sortCondition);\n for(int i=0;i<26;i++)\n {\n ans+=ceil(static_cast<double>(i+1)/8) * pq[i].second;\n }\n return ans;\n }\n};\n\n// sort based on the frequency of each character in descending order\n// then to mod 9 arrange then to each character", "memory": "15810" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "bool sortCondition(pair<int,int> &pq1,pair<int,int> &pq2)\n{\n return pq1.second>pq2.second;\n}\nclass Solution {\npublic:\n int minimumPushes(string &word) {\n int ans=0;\n vector<pair<int,int>> pq;\n for(int i=0;i<26;i++)\n {\n pq.push_back({i,0});\n }\n for(auto chr:word)\n {\n pq[chr-'a']={chr-'a',pq[chr-'a'].second+1};\n }\n sort(pq.begin(),pq.end(),sortCondition);\n // for(int i=0;i<26;i++)\n // {\n // pair<int,int> temp=pq[i];\n // cout << char('a'+temp.first) << \" \" << temp.second << endl;\n // }\n for(int i=0;i<26;i++)\n {\n ans+=ceil(static_cast<double>(i+1)/8) * pq[i].second;\n }\n return ans;\n }\n};\n\n// sort based on the frequency of each character in descending order\n// then to mod 9 arrange then to each character", "memory": "16070" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const std::string& word) {\n std::unordered_map<char, int> freqMap;\n\n // Count frequencies of each character\n for (const char& ch : word) {\n freqMap[ch]++;\n }\n\n // Create a vector to store frequencies\n std::vector<int> frequencies;\n frequencies.reserve(freqMap.size());\n\n for (const auto& entry : freqMap) {\n frequencies.push_back(entry.second);\n }\n\n // Sort the frequencies in descending order\n std::sort(frequencies.begin(), frequencies.end(), std::greater<int>());\n\n int ans = 0;\n int batchSize = 8;\n int pushes = 0;\n\n // Process frequencies in batches\n for (int i = 0; i < frequencies.size(); ++i) {\n if (i % batchSize == 0) {\n ++pushes; // Increment pushes for a new batch\n }\n ans += frequencies[i] * pushes;\n }\n\n return ans;\n }\n};", "memory": "16330" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n // count the frequencies\n vector<unsigned> count(26, 0);\n for (const char c : word) {\n ++count.at(c - 'a');\n // ++count[c - 'a'];\n }\n \n // create a maxHeap out of the frequencies\n priority_queue<pair<unsigned, char>> freq;\n for (int i = 0; i < 26; ++i)\n if (count[i] != 0)\n freq.emplace(count[i], i + 'a');\n \n // debug\n auto Freq = freq;\n while (!Freq.empty()) {\n const auto &[count, letter] = Freq.top();\n // cout << \"'\" << letter << \"': \" << count << endl;\n\n Freq.pop();\n }\n\n // create a tally\n // 1 press for each of the first 8 letters\n // 2 presses for each of the next 8 letters\n // 3 presses for each of the next 8 letters\n // 4 presses for each of the remaining 2 letters\n int total = 0;\n int heapIndex = 0;\n int multiplier = 1;\n while (!freq.empty()) {\n const auto &[count, letter] = freq.top();\n total += multiplier * count;\n\n // cout << letter << \": +\" << (multiplier * count) << \" presses\\n\";\n\n if (++heapIndex == 8 || heapIndex == 16 || heapIndex == 24)\n ++multiplier;\n \n freq.pop();\n }\n\n return total;\n }\n};\n\nint init = [] {\n // ios_base::sync_with_stdio(false);\n // cin.tie(NULL);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n int i = 0;\n for (string line; getline(cin, line); cout << endl) {\n if (line.front() == '\"' && line.back() == '\"')\n line = line.substr(1, line.length() - 2);\n\n Solution s;\n cout << s.minimumPushes(line);\n }\n\n exit(0);\n return 0;\n}();\n", "memory": "16330" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n vector<int> mapping(26, 0);\n const int SZ { int(word.size()) };\n for (int i = 0; i < SZ; i++)\n mapping[word[i] - 'a']++;\n multiset<int, std::greater<int>> remapping;\n for (char i = 0; i < 26; i++)\n if (mapping[i] > 0)\n remapping.emplace(mapping[i]);\n int ret {};\n char cnt {};\n for (auto iter = remapping.begin(); iter != remapping.end(); iter++)\n ret += *iter * (cnt++ / 8 + 1);\n return ret;\n }\n};", "memory": "16590" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n// naive solution\n// idea - the most frequent letters should cost less pushes\n// \n// count frequencies of each letter in word\n// sort letters by their frequencies in descending order\n// 'place' the most frequent letters as close to the beginnning in button as possible\n\n// xyzaabbccddeeffffggggghhhhhiiiiiiii\n// x-1 y-1 z-1 a-2 b-2 c-2 e-2 f-4 g-5 h-5 i-8\n\n// i-8 g-5 h-5 f-4 a-2 b-2 c-2 e-2 x-1 y-1 z-1\n\n\n// 1 2 ix 3 gy\n\n// 4 hz 5 f 6 a\n\n// 7 b 8 c 9 e \n\n// * 0 #\n\n// i-1 g-1 h-1 f-1 a-1 b-1 c-1 e-1 x-2 y-2 z-2 \n\n// time complexety O(n) \n// memory required O(1)\n int minimumPushes(const std::string& word) {\n static constexpr int alphabetSize = 26;\n\n // count frequencies\n std::unordered_map<char, int> frequencies;\n frequencies.reserve(alphabetSize);\n\n for (char letter : word) {\n if (frequencies.find(letter) == frequencies.end()) {\n frequencies.insert({letter, 0});\n }\n\n ++(frequencies.at(letter));\n }\n\n // sort by frequency\n // TODO: use std::array because size is always 26\n std::vector<std::pair<char, int>> frequenciesSorted(frequencies.begin(), frequencies.end());\n sort(frequenciesSorted.begin(),\n frequenciesSorted.end(),\n [](const std::pair<char, int>& left, const std::pair<char, int>& right) -> bool {\n return left.second > right.second;\n });\n\n // 0 1 2 3 4 5 6 7 8 9 10\n // i-1 g-1 h-1 f-1 a-1 b-1 c-1 e-1 x-2 y-2 z-2 \n\n // counter number of pushes\n int counter = 0;\n const int size1 = 8 < frequenciesSorted.size() ? 8 : frequenciesSorted.size();\n for (int i = 0; i < size1; ++i) {\n counter += (1 * frequencies.at(frequenciesSorted[i].first));\n }\n const int size2 = 16 < frequenciesSorted.size() ? 16 : frequenciesSorted.size();\n for (int i = size1; i < size2; ++i) {\n counter += (2 * frequencies.at(frequenciesSorted[i].first));\n }\n const int size3 = 24 < frequenciesSorted.size() ? 24 : frequenciesSorted.size();\n for (int i = size2; i < size3; ++i) {\n counter += (3 * frequencies.at(frequenciesSorted[i].first));\n }\n const int size4 = frequenciesSorted.size();\n for (int i = size3; i < size4; ++i) {\n counter += (4 * frequencies.at(frequenciesSorted[i].first));\n }\n \n\n return counter;\n }\n};", "memory": "16590" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const string &word) {\n\n unordered_map<char, int> counter;\n for (char ch : word)\n counter[ch]++;\n\n vector<pair<char, int>> VecCounter(counter.begin(), counter.end());\n // Use a lambda function for sorting\n sort(VecCounter.begin(), VecCounter.end(), [](const pair<char, int> &a, const pair<char, int> &b) {\n return a.second > b.second;\n });\n\n int minPushes = 0;\n for (size_t i = 0; i < VecCounter.size(); i++) {\n int round = i / 8;\n minPushes += (round + 1) * VecCounter[i].second;\n }\n return minPushes;\n }\n};", "memory": "16850" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(std::string_view word) {\n std::unordered_map<char, int> counts;\n for(char c : word)\n counts[c]++;\n \n std::priority_queue<int> pq;\n for(auto& p : counts)\n pq.push(p.second);\n int pressCount{ 0 };\n int usedSpots{ 0 };\n while(!pq.empty()) {\n int num = pq.top();\n pq.pop();\n pressCount += (1 + (int)(usedSpots * 0.125)) * num;\n ++usedSpots;\n }\n\n return pressCount;\n }\n};", "memory": "17110" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n array<int, 26> cnt{};\n for (char c : word) ++cnt[c - 'a'];\n sort(cnt.rbegin(), cnt.rend());\n int res = 0, i = 0;\n for (int n : cnt) res += n * ((i++ >> 3) + 1);\n return res;\n }\n};", "memory": "17370" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minimumPushes(string& word) {\n array<int, 26> cnt{};\n for (char c : word) ++cnt[c - 'a'];\n sort(cnt.rbegin(), cnt.rend());\n return transform_reduce(cnt.begin(), cnt.end(), 0, plus{}, [cnt = &cnt[0]](const int& x) { \n int i = &x - cnt;\n return x * ((i >> 3) + 1);\n });\n }\n};", "memory": "17370" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minimumPushes(string& word) {\n array<int, 26> cnt{};\n for (char c : word) ++cnt[c - 'a'];\n sort(cnt.rbegin(), cnt.rend());\n int res = 0, i = 0;\n for (int n : cnt) res += n * ((i++ >> 3) + 1);\n return res;\n }\n};", "memory": "17630" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#include <unordered_map>\n#include <vector>\n#include <algorithm>\n#include <string>\n\nclass Solution {\npublic:\n static bool cmp(const std::pair<char, int>& a, const std::pair<char, int>& b) {\n return a.second > b.second;\n }\n\n int minimumPushes(const std::string& word) {\n std::unordered_map<char, int> map;\n for (char c : word) {\n map[c]++;\n }\n \n // Convert unordered_map to vector of pairs\n std::vector<std::pair<char, int>> vec(map.begin(), map.end());\n \n // Sort the vector based on the frequency\n std::sort(vec.begin(), vec.end(), cmp);\n \n int a = 2;\n int b = 1;\n int x = 0;\n \n for (const auto& c : vec) {\n if (a > 9) {\n a = 3;\n b++;\n x += b*c.second;\n } else {\n a++;\n x += b*c.second;\n }\n }\n \n return x;\n }\n};\n", "memory": "17630" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const std::string& word) {\n // Count frequency of each character\n std::unordered_map<char, int> freq;\n for (char c : word) {\n freq[c]++;\n }\n\n // Sort characters by frequency in descending order\n std::vector<std::pair<char, int>> wordfreq(freq.begin(), freq.end());\n std::sort(wordfreq.begin(), wordfreq.end(),\n [](const auto& a, const auto& b) { return a.second > b.second; });\n\n // Initialize presses for each button\n std::array<int, 5> presses = {8, 8, 8, 8, 8};\n\n // Calculate total presses\n int totalPresses = 0;\n for (const auto& pair : wordfreq) {\n int frequency = pair.second;\n for (int i = 0; i < 5; ++i) {\n if (presses[i] > 0) {\n totalPresses += frequency * (i + 1);\n presses[i]--;\n break;\n }\n }\n }\n\n return totalPresses;\n }\n};", "memory": "17890" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const string& word) {\n vector<int> frequency(26, 0);\n for (char c : word) {\n frequency[c - 'a']++;\n }\n \n vector<int> freqs;\n for (int freq : frequency) {\n if (freq > 0) {\n freqs.push_back(freq);\n }\n }\n sort(freqs.rbegin(), freqs.rend());\n \n int totalPushes = 0;\n int keyPress = 1;\n int lettersInKey = 0;\n int keysAvailable = 8;\n \n for (int freq : freqs) {\n totalPushes += freq * keyPress;\n lettersInKey++;\n if (lettersInKey == keysAvailable) {\n keyPress++;\n lettersInKey = 0;\n }\n }\n \n return totalPushes;\n}\n\n};", "memory": "17890" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const std::string& word) {\n std::vector<int> freq(26, 0);\n\n // Count the frequency of each letter\n for (char c : word) {\n freq[c - 'a']++;\n }\n\n // Sort frequencies in descending order\n std::sort(freq.rbegin(), freq.rend());\n\n int minPresses = 0;\n\n // Calculate minimum presses using division for cycling\n for (int i = 0; i < 26; ++i) {\n if (freq[i] == 0) break;\n int presses = (i /8) + 1; // Cycle presses from 1 to 8\n minPresses += freq[i] * presses;\n }\n\n return minPresses;\n }\n};\n", "memory": "18150" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n vector<int> ans(26,0);\n for(int i = 0;i<word.size();i++){\n ans[word[i] - 'a']++;\n }\n sort(ans.rbegin(), ans.rend());\n int res = 0;\n int next = 0;\n for(int i = 0; i<26 && ans[i]!=0; i++){\n if (i%8==0) next++;\n res += ans[i]*next; \n }\n return res;\n }\n};", "memory": "18410" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution\n{\npublic:\n int minimumPushes(string& word)\n {\n vector<int> freq(26);\n\n for (char c : word)\n {\n freq[c - 'a']++;\n }\n \n sort(freq.rbegin(), freq.rend());\n int result = 0;\n for (int i = 0; i < freq.size(); i++)\n {\n if (freq[i] == 0)\n continue;\n\n result += freq[i] * (i / 8 + 1);\n }\n\n return result;\n }\n};", "memory": "18410" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n static int minimumPushes(string& word) {\n vector<int>mp(26,0);\n for(char &ch: word){\n mp[ch-'a']++;\n }\n sort(mp.rbegin(),mp.rend());//decending order\n int result=0;\n\n for(int i=0;i<26;i++){\n int freq=mp[i];\n int press=(i/8+1);\n result+=(press*freq);\n }\n return result;\n }\n};\n", "memory": "18670" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#include <unordered_map>\n#include <string>\n\nclass Solution {\npublic:\n int minimumPushes(const std::string& word) {\n int result = 0;\n vector<int> vec(26,0);\n\n for(auto it:word){\n vec[it-'a']++;\n }\n\n sort(vec.rbegin(), vec.rend());\n for(int i=0; i<vec.size(); i++){\n int fre = vec[i];\n int position = (i/8) + 1;\n result += (fre*position); \n\n }\n return result;\n }\n};\n", "memory": "18670" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> cnt(26);\n for(auto &i : word) {\n ++cnt[i - 'a'];\n }\n sort(cnt.begin(), cnt.end(), greater<int>());\n int ans = 0;\n for(int i = 0; i < 26; ++i) {\n if (cnt[i] == 0)\n break;\n ans += (i / 8 + 1) * cnt[i];\n }\n return ans;\n }\n};", "memory": "20490" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "using namespace std;\n\nclass Solution {\nprivate:\n int solve(const vector<pair<char, int>>& temp) {\n int ans = 0;\n int cnt = 1;\n int size = temp.size();\n int eight=8;\n for (int i = 0; i < size; i++) {\n if (eight==0)\n {\n eight=8;\n cnt++;\n }\n\n ans += cnt * temp[i].second;\n eight--;\n }\n return ans;\n }\n\npublic:\n static bool cmp(const pair<char, int>& a, const pair<char, int>& b) {\n return a.second > b.second;\n }\n\n int minimumPushes(const string& word) {\n map<char, int> mp;\n for (char it : word) {\n mp[it]++;\n }\n \n vector<pair<char, int>> temp;\n for (const auto& it : mp) {\n temp.push_back({it.first, it.second});\n }\n \n sort(temp.begin(), temp.end(), cmp);\n\n // Optional: Print sorted vector for debugging\n for (const auto& it : temp) {\n cout << it.first << \" \" << it.second << endl;\n }\n\n int size = temp.size();\n int ans = 0;\n\n if (size <= 8) {\n ans = word.length();\n } else {\n ans = solve(temp); \n }\n\n return ans;\n }\n};", "memory": "20490" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> f(26);\n for(auto x:word){\n f[x-'a']++;\n }\n sort(f.begin(),f.end());\n reverse(f.begin(),f.end());\n int ans = 0;\n int ii = 0;\n for(int i=0;i<26;i++){\n if(i%8==0){ii++;}\n ans = ans + f[i]*ii;\n }\n return ans;\n }\n};", "memory": "20750" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> cnt(26, 0);\n for (auto& ch : word)\n cnt[ch - 'a']++;\n \n int res = 0;\n sort(cnt.begin(), cnt.end(), greater<int>());\n for (int i = 0; i < 26; i++) {\n res += (i / 8 + 1) * cnt[i];\n }\n \n return res;\n }\n};", "memory": "21010" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int>v(26,0);\n for(int i=0;i<word.size();i++)\n {\n v[word[i]-'a']++;\n }\n sort(v.begin(),v.end(),greater<int>());\n \n int ans=0;\n int temp=0;\n for(auto i=0;i<v.size();i++)\n {\n // cout<<i<<\" \"<<ceil(i/8+1)<<endl;\n ans+=v[i]*(ceil(i/8+1));\n }\n return ans;\n }\n};", "memory": "21270" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) \n {\n vector<int> mp(26,0);\n for(char &ch:word)\n {\n mp[ch-'a']++;\n }\n sort(begin(mp),end(mp), greater<int>());\n\n int result = 0;\n for(int i=0;i<26;i++)\n {\n int freq=mp[i];\n int press = i/8+1;\n\n result += press*freq;\n }\n return result;\n }\n};", "memory": "21530" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> f(26,0);\n for(int i=0;i<word.length();i++)\n f[word[i]-'a']++;\n int ans=0;\n sort(f.begin(),f.end(),greater<int>());\n for(int i=0;i<26;i++){\n if(i<8)\n ans+=f[i];\n else if(i<16)\n ans+=f[i]*2;\n else if(i<24)\n ans+=f[i]*3;\n else\n ans+=f[i]*4;\n }\n return ans;\n }\n};", "memory": "21530" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int>mp(26,0);\n for(char &ch:word){\n mp[ch-'a']++;\n }\n sort(begin(mp),end(mp),greater<int>());//sorting in decending order\n int res=0;\n for(int i=0;i<26;i++){\n int freq=mp[i];\n int press=(i/8+1);\n res+=(freq*press);\n }\n return res;\n }\n};", "memory": "21790" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int ans=0;\n vector<int> freq(26,0);\n for(auto it: word){\n freq[it-'a']++;\n }\n sort(freq.begin(),freq.end(),greater<int>());\n for(int i=0;i<26;i++){\n if(i<8){\n ans+= freq[i];\n }\n else if(i<16){\n ans+= freq[i]*2; \n }else if(i<24){\n ans+= freq[i]*3; \n }\n else ans+= freq[i]*4;\n }\n return ans;\n }\n};", "memory": "21790" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\n public:\n int minimumPushes(string word) {\n int ans = 0;\n vector<int> count(26);\n\n for (const char c : word)\n ++count[c - 'a'];\n\n ranges::sort(count, greater<>());\n\n for (int i = 0; i < 26; ++i)\n ans += count[i] * (i / 8 + 1);\n\n return ans;\n }\n};", "memory": "22050" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "#include<bits/stdc++.h>\n\nclass Solution {\npublic:\n\n\n int minimumPushes(string word) {\n \n vector<int> freq(26, 0);\n for( auto x: word ) \n freq[x-'a']++;\n \n sort( freq.begin(), freq.end(), greater<int>());\n\n int n=0, ans=0, seg=1;\n for( int i=0; i<freq.size(); i++ ) {\n // cout<<x<<endl;\n n++;\n if( freq[i]==0 ) break;\n \n seg = ceil(float(n)/float(8));\n ans += freq[i]*seg;\n\n }\n\n return ans;\n }\n};", "memory": "22310" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "#include<bits/stdc++.h>\n\nclass Solution {\npublic:\n\n\n int minimumPushes(string word) {\n \n vector<int> freq(26, 0);\n for( auto x: word ) \n freq[x-'a']++;\n \n sort( freq.begin(), freq.end(), greater<int>());\n\n int n=0, ans=0, seg=1;\n for( int i=0; i<freq.size(); i++ ) {\n // cout<<x<<endl;\n n++;\n if( freq[i]==0 ) break;\n \n seg = ceil(float(n)/float(8));\n ans += freq[i]*seg;\n\n }\n\n return ans;\n }\n};", "memory": "22310" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n\n int arr[27] = {0};\n //count the occurence, put to array\n for (int i=0;i<word.length();i++) {\n arr[word[i]-'a']++;\n }\n \n //iterate the array, put to vector\n vector<int> v;\n for(int i=0;i<27;i++) {\n if(arr[i]!=0) {\n v.push_back(arr[i]);\n }\n }\n\n //sort the vector to the number of occurence\n sort(v.begin(),v.end(), greater<int>());\n\n //loop the vector to count the result\n\n\n int magnitude = 1;\n int countKeyMapped = 0;\n\n int res = 0;\n for (int i=0;i<v.size();i++) {\n countKeyMapped +=1;\n if(countKeyMapped == 9) {\n countKeyMapped = 1;\n magnitude++;\n }\n res+=magnitude*v[i];\n\n }\n\n return res;\n }\n};", "memory": "22570" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> freq(26);\n for(char s:word)\n {\n freq[s-'a']++;\n }\n vector<int> p;\n for(int i=0;i<26;i++)\n {\n if(freq[i])\n p.push_back(freq[i]);\n }\n sort(p.begin(),p.end(),greater<int>());\n int n = p.size();\n int count = 0;\n int ans =0;\n for(int i=0;i<n;i++)\n {\n ans+= (count/8 + 1)*p[i];\n count++;\n }\n return ans;\n }\n};", "memory": "22830" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n const int available_keys = 8;\n\n vector<int> alpha(26, 0);\n for (char ch : word)\n alpha[ch - 'a']++;\n\n vector<int> f;\n for (int i=0; i<26; i++)\n {\n if (alpha[i])\n f.push_back(alpha[i]);\n }\n sort(f.begin(), f.end(), std::greater<int>());\n\n int i=0, nxt, res = 0, press = 1, m = f.size();\n\n while (i<m)\n {\n for (nxt = min(i+available_keys, m); i < nxt; i++) // 8 'cause [2..9] = 8\n res += f[i] * press;\n\n press++;\n }\n\n return res;\n }\n};", "memory": "23090" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "constexpr size_t SIZE = 'z' + 1;\nconst size_t CLICKABLE_BUTTONS = 8;\nconst size_t MAX_LETTERS_IN_BUTTON = 4;\nclass Solution {\npublic:\n int minimumPushes(string word) {\n array<int, SIZE> arr{};\n for(const char& c : word){\n ++arr[c];\n }\n\n sort(arr.begin() + 'a', arr.end());\n array<int, MAX_LETTERS_IN_BUTTON + 1> clickable_at_letter_taken{};\n int result = 0;\n\n for(char arr_index = 'z'; arr_index >= 'a'; --arr_index){\n for(int i = 1; i < clickable_at_letter_taken.size(); ++i){\n if(clickable_at_letter_taken[i] < CLICKABLE_BUTTONS){\n ++clickable_at_letter_taken[i];\n result += i * arr[arr_index];\n break;\n }\n }\n }\n\n return result;\n }\n};", "memory": "23350" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int n = word.size();\n vector<int>freq(26,0), typeCount(26,0);\n vector<pair<int,int>>freqLetter(26);\n\n int uniqueLetterCount = 0, totalPushNeeded = 0;\n \n for(auto ch : word) {\n freq[ch - 'a']++;\n }\n \n for(int i = 0; i < 26; i++) {\n freqLetter[i] = {freq[i], i};\n }\n\n sort(freqLetter.begin(), freqLetter.end(), greater<pair<int,int>>());\n\n for(int i = 0; i < 26; i++) {\n if(freqLetter[i].first == 0) {\n continue;\n }\n if(typeCount[freqLetter[i].second] == 0) {\n uniqueLetterCount++;\n int pushNeeded = (uniqueLetterCount/8) + (uniqueLetterCount % 8 > 0 ? 1 : 0);\n typeCount[freqLetter[i].second] = pushNeeded;\n }\n totalPushNeeded += (freqLetter[i].first * typeCount[freqLetter[i].second]);\n }\n return totalPushNeeded;\n }\n};", "memory": "23610" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string str) {\n vector<int> word(26, 0);\n vector<pair<int, int> > sorted_word;\n for(auto ch: str){\n word[ch - 'a']++;\n }\n for(int i = 0; i < 26; i++){\n if(word[i] > 0) {\n sorted_word.push_back({word[i], i});\n }\n }\n sort(sorted_word.begin(), sorted_word.end(), greater<pair<int, int> >());\n\n int count = 0; int val = 1;\n for(int i = 0; i < sorted_word.size(); i++) {\n word[sorted_word[i].second] = val;\n count++;\n if(count == 8){\n val++;\n count = 0;\n }\n }\n int ans = 0;\n for(auto ch: str) ans += word[ch - 'a'];\n \n\n\n return ans;\n }\n};", "memory": "23870" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) { \n\n int arr[26];\n int n = word.size();\n for(int i=0;i<n;i++) {\n arr[word[i]-'a']++;\n }\n \n priority_queue<int>pq;\n for(int i=0;i<26;i++) {\n if(arr[i]!=0) {\n pq.push(arr[i]);\n }\n \n }\n\n int ans=0,i=0;\n while(!pq.empty()) {\n int freq = pq.top();\n if(i<8) {\n ans+=(freq);\n } else if(i<16) {\n ans+=(freq*2);\n } else if(i<24) {\n ans+=(freq*3);\n } else ans+=(freq*4);\n i++;\n pq.pop();\n }\n return ans;\n }\n};", "memory": "24130" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> cntChar;\n priority_queue<pair<int,int>> pq;\n int total = 0,\n type = 1,\n cnt = 1;\n \n cntChar.resize(26,0);\n for(char w: word) cntChar[w-'a']++;\n for(int i=0; i<26; i++){\n pq.push({cntChar[i], i});\n }\n while(pq.size()> 0){\n total += pq.top().first*type;\n pq.pop();\n cnt++;\n if(cnt> 8){\n cnt = 1;\n type++;\n }\n }\n return total;\n }\n};", "memory": "25950" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "#include <unordered_map>\nclass Solution {\npublic:\n int minimumPushes(string word) {\n std::unordered_map<char, int> myMap;\n int lastIndex = word.length()-1;\n std::string myWord;\n int wordIndex = 0;\n //Store each character in word and its frequency in a hashmap\n for(int index =0;index<=lastIndex;index++)\n {\n if(myMap.find(word[index]) != myMap.end())\n myMap[word[index]]=myMap[word[index]]+1;\n else\n {\n myMap[word[index]]=1;\n myWord.push_back(word[index]);\n } \n }\n wordIndex = myWord.size()-1;\n char temp;\n for(int a =0; a <= wordIndex; a++)\n {\n for(int index =0; index <= wordIndex-a-1; index++)\n {\n if(myMap[myWord[index]]<myMap[myWord[index+1]])\n {\n temp=myWord[index];\n myWord[index]= myWord[index+1];\n myWord[index+1]=temp;\n }\n }\n } \n //calculate cost\n int cost=0;\n for(int a=0; a<=wordIndex; a++)\n {\n //cost of one tap (1 + a/8)\n //frequency myMap[myWord[a]]\n cost=cost + (1 + a/8)*myMap[myWord[a]];\n }\n return cost;\n }\n};", "memory": "26210" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "#include <unordered_map>\nclass Solution {\npublic:\n int minimumPushes(string word) {\n std::unordered_map<char, int> myMap;\n int lastIndex = word.length()-1;\n std::string myWord;\n int wordIndex = 0;\n int index=0;\n //Store each character in word and its frequency in a hashmap\n for( index =0;index<=lastIndex;index++)\n {\n if(myMap.find(word[index]) != myMap.end())\n myMap[word[index]]=myMap[word[index]]+1;\n else\n {\n myMap[word[index]]=1;\n myWord.push_back(word[index]);\n } \n }\n wordIndex = myWord.size()-1;\n char temp;\n for(int a =0; a <= wordIndex; a++)\n {\n for( index =0; index <= wordIndex-a-1; index++)\n {\n if(myMap[myWord[index]]<myMap[myWord[index+1]])\n {\n temp=myWord[index];\n myWord[index]= myWord[index+1];\n myWord[index+1]=temp;\n }\n }\n } \n //calculate cost\n int cost=0;\n for( index=0; index<=wordIndex; index++)\n {\n //cost of one tap (1 + a/8)\n //frequency myMap[myWord[a]]\n cost=cost + (1 + index/8)*myMap[myWord[index]];\n }\n return cost;\n }\n};", "memory": "26210" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "#include <unordered_map>\n#include<iostream>\nclass Solution {\npublic:\n int minimumPushes(string word) {\n std::unordered_map<char, int> myMap;\n int lastIndex = word.length()-1;\n std::string myWord;\n int wordIndex = 0;\n //Store each character of word in a hashmap\n for(int index =0;index<=lastIndex;index++)\n {\n if(myMap.find(word[index]) == myMap.end())\n {\n myMap[word[index]]=1;\n myWord.push_back(word[index]);\n wordIndex=wordIndex+1;\n }\n else\n myMap[word[index]]=myMap[word[index]]+1;\n }\n\n wordIndex = wordIndex -1;\n char temp;\n for(int a =0; a <= wordIndex; a++)\n {\n for(int index =0; index <= wordIndex-a-1; index++)\n {\n if(myMap[myWord[index]]<myMap[myWord[index+1]])\n {\n temp=myWord[index];\n myWord[index]= myWord[index+1];\n myWord[index+1]=temp;\n }\n }\n } \n //calculate cost\n int cost=0;\n for(int a=0; a<=wordIndex; a++)\n {\n //cost of one tap (1 + a/8)\n //frequency myMap[myWord[a]]\n cost=cost + (1 + a/8)*myMap[myWord[a]];\n }\n return cost;\n }\n};", "memory": "26470" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "#include <unordered_map>\n#include<iostream>\nclass Solution {\npublic:\n int minimumPushes(string word) {\n std::unordered_map<char, int> myMap;\n int lastIndex = word.length()-1;\n std::string myWord;\n int wordIndex = 0;\n //Store each character in word and its frequency in a hashmap\n for(int index =0;index<=lastIndex;index++)\n {\n if(myMap.find(word[index]) != myMap.end())\n myMap[word[index]]=myMap[word[index]]+1;\n else\n {\n myMap[word[index]]=1;\n myWord.push_back(word[index]);\n //wordIndex=wordIndex+1;\n } \n }\n wordIndex = myWord.size()-1;\n char temp;\n for(int a =0; a <= wordIndex; a++)\n {\n for(int index =0; index <= wordIndex-a-1; index++)\n {\n if(myMap[myWord[index]]<myMap[myWord[index+1]])\n {\n temp=myWord[index];\n myWord[index]= myWord[index+1];\n myWord[index+1]=temp;\n }\n }\n } \n //calculate cost\n int cost=0;\n for(int a=0; a<=wordIndex; a++)\n {\n //cost of one tap (1 + a/8)\n //frequency myMap[myWord[a]]\n cost=cost + (1 + a/8)*myMap[myWord[a]];\n }\n return cost;\n }\n};", "memory": "26470" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\n\npublic:\n\n int minimumPushes(string word) {\n\n // Frequency map to store count of each letter\n\n unordered_map<char, int> frequencyMap;\n\n // Count occurrences of each letter\n\n for (char& c : word) {\n\n ++frequencyMap[c];\n\n }\n\n // Priority queue to store frequencies in descending order\n\n priority_queue<int> frequencyQueue;\n\n // Push all frequencies into the priority queue\n\n for (const auto& entry : frequencyMap) {\n\n frequencyQueue.push(entry.second);\n\n }\n\n int totalPushes = 0;\n\n int index = 0;\n\n // Calculate total number of presses\n\n while (!frequencyQueue.empty()) {\n\n totalPushes += (1 + (index / 8)) * frequencyQueue.top();\n\n frequencyQueue.pop();\n\n index++;\n\n }\n\n return totalPushes;\n\n }\n\n};\n\n\n \n \n", "memory": "26730" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n unordered_map<char, int> freq;\n for (char &c : word) {\n ++freq[c];\n }\n priority_queue<int> pq;\n for (auto &elem : freq) {\n pq.push(elem.second);\n }\n\n int res = 0;\n int idx = 0;\n while (!pq.empty()) {\n res += (idx/8 + 1) * pq.top();\n pq.pop();\n idx++;\n }\n return res;\n }\n};", "memory": "26990" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n unordered_map<char, int>mapping;\n for(auto ch:word){\n mapping[ch]++;\n }\n\n vector<int> v;\n for(auto p:mapping){\n v.push_back(p.second);\n cout<<p.second<<endl;\n }\n\n sort(v.begin(), v.end(), greater<int>());\n int ans=0, ct=0;\n for(auto p:v){\n ct++;\n if(ct<=8){\n ans+=1*p;\n }\n else if(ct<=16){\n ans+=2*p;\n }\n else if(ct<=24){\n ans+=3*p;\n }\n else{\n ans+=4*p;\n }\n }\n return ans;\n }\n};", "memory": "26990" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n \n unordered_map<char,int>mpp;\n for(char ch: word){\n mpp[ch]++;\n }\n priority_queue<pair<int, char>>pq;\n\n for(auto it:mpp){\n pq.push({it.second,it.first});\n }\n \n int cnt=2;\n int multi=1;\n int ans=0;\n \n while(!pq.empty()){\n \n auto digit=pq.top();\n int freq=digit.first;\n pq.pop();\n\n if(cnt==10){\n multi+=1;\n cnt=2;\n }\n \n ans+=freq*multi;\n cnt++; \n\n }\nreturn ans;\n\n }\n};", "memory": "27250" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int n = word.length();\n int len = 1;\n int ans = 0;\n\n unordered_map<char,int>mp;\n for(auto &it:word){\n mp[it]++;\n }\n\n vector<pair<int,char>>arr;\n for(auto &it:mp){\n arr.push_back({it.second,it.first});\n }\n\n sort(arr.begin(),arr.end());\n reverse(arr.begin(),arr.end());\n\n for (auto &it:arr) {\n if(len<9){\n ans+=it.first;\n len++;\n }\n else{\n int v = len/8;\n if(len%8 !=0){\n ans= ans + it.first*(v+1);\n }\n else{\n ans= ans + it.first*(v+0);\n }\n len++;\n }\n }\n return ans;\n \n }\n};", "memory": "27250" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n // Frequency vector to store count of each letter\n vector<int> frequency(26, 0);\n\n // Count occurrences of each letter\n for (char& c : word) {\n ++frequency[c - 'a'];\n }\n\n // Sort frequencies in descending order\n sort(frequency.rbegin(), frequency.rend());\n\n int totalPushes = 0;\n\n // Calculate total number of presses\n for (int i = 0; i < 26; ++i) {\n if (frequency[i] == 0) break;\n totalPushes += (i / 8 + 1) * frequency[i];\n }\n\n return totalPushes;\n }\n};", "memory": "27510" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int>freq(26,0);\n for(auto i:word)freq[i-'a']++;\n\n sort(freq.rbegin(),freq.rend());\n\n int ans=0;\n int d=1;\n for(auto i:freq){\n if(d<=8)ans+=i;\n else if(d<=16)ans+=(2*i);\n else if(d<=24) ans+=(3*i);\n else ans+=(4*i);\n d++;\n }\n return ans;\n }\n};", "memory": "27510" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> frequencies(26, 0);\n\n for (auto letter : word)\n {\n frequencies[letter - 'a']++;\n }\n\n sort(frequencies.rbegin(), frequencies.rend());\n\n int totalPushes = 0;\n\n for(int i = 0; i < frequencies.size(); i++)\n {\n if(frequencies[i] == 0)\n {\n break;\n }\n\n totalPushes += frequencies[i] * (i / 8 + 1);\n }\n\n return totalPushes;\n }\n};", "memory": "27770" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "#include <bits/stdc++.h> \nusing namespace std; \nbool cmp(pair<char,int>&a, pair<char,int>&b)\n{\n return a.second>b.second;\n}\n\n int solve(map<char, int>&m)\n {\n vector<pair<char,int>>v;\n for(auto &u:m)\n v.push_back(u);\n sort(v.begin(), v.end(),cmp);\n\n int cnt=1, sum=0;\n\n for(int i = 0; i<v.size(); i++)\n {\n \n sum += (v[i].second)*cnt;\n \n if((i+1)%8==0) cnt++;\n\n //cout<<v[i].second<<\" \"<<endl;\n }\n\n return sum;\n\n\n }\nclass Solution {\npublic:\n\n\n int minimumPushes(string word) {\n\n map<char, int>m;\n for(auto u:word)\n m[u]++;\n return solve(m);\n \n }\n};", "memory": "27770" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n map<int,int> mp;\n int total=0;\n int num=0;\n\n for(int i=0; i<word.size(); i++){\n mp[word[i]]++;\n }\n\n vector<pair<int, int>> vec(mp.begin(), mp.end());\n\n auto cmp = [](const pair<int, int>& a, const pair<int, int>& b) {\n return a.second > b.second;\n };\n std::sort(vec.begin(), vec.end(), cmp);\n\n for(auto& p : vec){\n num++;\n cout << p.first << \" \" << p.second << std::endl;\n total += p.second*(ceil(num/8.0));\n }\n\n return total;\n }\n};", "memory": "28030" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
3
{ "code": " bool comp(pair<char,int> a,pair<char,int> b){\n return b.second<a.second;\n\n }\nclass Solution {\npublic:\n \n int minimumPushes(string word) {\n map<char,int> map;\n for(int i=0;i<word.size();i++){\n map[word[i]]++;\n }\n int ans=0;\n int count=0;\n int w=1;\n int n=map.size();\n vector<pair<char,int>> myvec(map.begin(),map.end());\n sort(myvec.begin(),myvec.end(),comp);\n for(int i=0;i<myvec.size();i++){\n pair<char,int> x=myvec[i];\n cout<<x.first<<\" \"<<x.second<<endl;\n ans+=x.second*w;\n count++;\n if(count==8||count==16||count==24){\n w++;\n }\n }\n return ans;\n\n\n \n }\n};", "memory": "28030" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumPushes(string word) {\n map<char,int> mp;\n for(int i=0;i<word.size();i++){\n mp[word[i]]++;\n }\n priority_queue<pair<int,char>> pq;\n for(const auto & it :mp){\n pq.push({it.second,it.first});\n }\n int result=0;\n int count=1;\n while(!pq.empty()){\n pair<int,char> temp=pq.top();\n int top=temp.first;\n if(count<=8){\n result=result+1*top;\n }else if(count<=16){\n result=result+2*top;\n }else if(count<=24){\n result=result+3*top;\n }else{\n result=result+4*top;\n }\n pq.pop();\n count++;\n }\n return result;\n }\n};", "memory": "28290" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "int sumDigits(int num) {\n int sum = 0;\n while (num) {\n sum += num % 10;\n num /= 10;\n }\n return sum;\n}\n\nint init=[] {\n cin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n int k;\n for (std::string s; getline(cin, s);) {\n int sum = 0;\n for (auto it : s) {\n if(it == '\"')\n continue;\n int value = it - 'a' + 1;\n if (value >= 10) {\n sum += value % 10 + value / 10;\n } else\n sum = sum + value;\n }\n\n getline(cin, s);\n k = std::stoi(s);\n\n k--;\n while (k-- && sum >= 10) {\n sum = sumDigits(sum);\n }\n std::cout << sum << '\\n';\n }\n return 0;\n}\n();\n\nclass Solution {\npublic:\n int getLucky(string s, int k) { return 0; }\n};", "memory": "7500" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string& s, int k) {\n int num=0;\n for(char x : s) {\n int c = x-'a'+1;\n auto[q, r]=div(c,10);\n num+=q+r;\n }\n if (k==1) return num;\n k--;\n for(int c=num; c>=10 && k>0; k--){\n for(num=0; c>0; ){\n auto[q,r]=div(c, 10);\n num+=r;\n c=q;\n }\n c=num;\n }\n return num;\n }\n};", "memory": "7600" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string& s, int k) {\n int num=0;\n\n for (char c: s){\n int x=c-'a'+1;\n auto [q, r]=div(x, 10);\n num+=q+r;\n }\n\n if (k==1) return num;\n k--;\n for(int x=num; x>=10 && k>0; k--){\n for(num=0; x>0; ){\n auto [q, r]=div(x, 10);\n num+=r;\n x=q;\n }\n\n x=num;\n }\n return num;\n }\n};", "memory": "7600" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int total = accumulate(cbegin(s), cend(s), 0,\n [](const auto& total, const auto& x) {\n const auto num = x - 'a' + 1;\n return total + num / 10 + num % 10;\n });\n while (--k && total > 9) {\n int new_total = 0;\n for (; total; total /= 10) {\n new_total += total % 10;\n }\n total = new_total;\n }\n return total;\n }\n};", "memory": "7700" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n //transform letters, 1st transform\n int sum =0;\n for(char cv:s){\n int d = ( cv - 'a' + 1);\n while(d){\n sum += (d % 10);\n d /= 10;\n }\n }\n \n int sumo = sum;\n for(int i=1;i<k;i++){\n sumo =0;\n while(sum){\n sumo += (sum %10);\n sum /=10;\n }\n sum = sumo;\n }\n return sumo;\n \n }\n};", "memory": "7700" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int transform(int num) {\n int ans = 0;\n while (num) {\n ans += num%10;\n num /= 10;\n }\n return ans;\n }\n int getLucky(string s, int k) {\n int num = 0;\n k--;\n for (auto &c : s) {\n int x = c-'a'+1;\n num += x%10 + x/10;\n }\n while (k--) num = transform(num);\n return num;\n }\n};", "memory": "7800" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string& s, int k) {\n int num=0;\n // convert letters to digits\n for (char c: s){\n int x=c-'a'+1;\n auto [q, r]=div(x, 10);// x may has 1~2 digits\n num+=q+r;\n }\n // cout<<num<<endl;\n if (k==1) return num;\n k--;\n for(int x=num; x>=10 && k>0; k--){\n for(num=0; x>0; ){\n auto [q, r]=div(x, 10);\n num+=r;\n x=q;\n }\n // cout<<\"k=\"<<k<<\", num=\"<<num<<endl;\n x=num;\n }\n return num;\n }\n};", "memory": "7800" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n long long int currNum = 0;\n for (auto ch : s) {\n auto num = ch - 'a' + 1;\n while (num) {\n currNum += num % 10;\n num = num / 10;\n }\n }\n\n long long int nextNum = currNum;\n while (k > 1) {\n nextNum = currNum;\n currNum = 0;\n while (nextNum > 0) {\n currNum += (nextNum % 10);\n nextNum = nextNum / 10;\n }\n k--;\n }\n return currNum;\n }\n};", "memory": "7900" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int sum = 0;\n for (char c : s) {\n int num = c - 'a' + 1;\n while (num > 0) {\n sum =sum+ num % 10; \n num =num/ 10; \n }\n }\n while (--k > 0) {\n int newSum = 0;\n while (sum > 0) {\n newSum =newSum+sum % 10; \n sum =sum/ 10; \n }\n sum = newSum;\n }\n\n return sum;\n }\n};", "memory": "7900" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int sum = 0;\n\n for (char ch : s) {\n int val = (int)ch - 'a' + 1;\n while (val > 0) {\n sum += val % 10; \n val /= 10;\n }\n }\n\n for (int i = 1; i < k; i++) {\n int newSum = 0;\n while (sum > 0) {\n newSum += sum % 10; \n sum /= 10;\n }\n sum = newSum; \n }\n\n return sum;\n }\n};\n", "memory": "8000" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getSum(int n)\n {\n int sum =0;\n while(n > 0)\n {\n sum += n%10;\n n /= 10;\n }\n return sum;\n }\n int getLucky(string s, int k) {\n int num =0;\n for(auto c: s)\n {\n int digit = c - 'a' + 1;\n num += getSum(digit);\n }\n k--;\n while(k--)\n {\n num = getSum(num);\n }\n return num;\n }\n};", "memory": "8000" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n vector<int> positions{};\n int answer{};\n\n for(size_t i{}; i < s.size(); i++)\n {\n char c = s.at(i);\n positions.push_back(c - 'a' + 1);\n }\n\n while(k)\n {\n for(size_t i{}; i < positions.size(); i++)\n {\n if(positions.at(i) >= 10)\n {\n int rem = positions.at(i) % 10;\n int div = positions.at(i) / 10;\n answer += rem + div;\n }\n else\n answer += positions.at(i);\n }\n k--;\n\n if(k == 0)\n return answer;\n else\n {\n positions.clear();\n while(answer)\n {\n int rem = answer % 10;\n positions.push_back(rem);\n answer /= 10;\n }\n }\n \n }\n\n return -1;\n }\n};", "memory": "8100" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int brokenSum(int a) {\n int ans = 0;\n while (a != 0) {\n int r = a % 10;\n ans += r;\n a /= 10;\n }\n return ans;\n }\n\n int getLucky(string s, int k) {\n vector<int> ans;\n int i = 0;\n\n // Convert characters to their respective numbers and store in vector\n while (i < s.length()) {\n int val = s[i] - 'a' + 1;\n ans.push_back(val);\n i++;\n }\n\n // Compute the initial sum from the vector\n int sum = 0;\n for(int i=0;i<ans.size();i++){\n if(ans[i]>=10){\n sum += brokenSum(ans[i]);\n }else{\n sum += ans[i];\n }\n }\n int j =1;\n\n //applying brokenSum logic k times\n while(j<k){\n sum = brokenSum(sum);\n j++;\n }\n\n return sum;\n }\n};\n", "memory": "8100" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8};\n\n int sum = 0;\n for(int i=0; i<s.length(); i++)\n {\n int index = s[i] - 'a';\n sum += arr[index];\n }\n k--;\n\n while(k!=0)\n {\n int num = sum;\n sum = 0;\n while(num!=0)\n {\n sum += num%10;\n num /= 10;\n }\n k--;\n if(sum < 10)\n {\n break;\n }\n }\n\n return sum;\n }\n};", "memory": "8200" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n vector<int> nums;\n for(int i=0;i<s.length();i++){\n nums.push_back(s[i]-'a'+1);\n } \n long long int num=0;\n for(int i=0;i<nums.size();i++){\n while(nums[i]>0){\n num+=nums[i]%10;\n nums[i]=nums[i]/10;\n }\n }\n long long int ans=0;\n for(int i=0;i<k-1;i++){\n while(num>0){\n ans+=num%10;\n num=num/10;\n }\n num=ans;\n ans=0;\n }\n return num;\n }\n};", "memory": "8200" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n vector<int> arr(s.size());\n\n // for(int i=0;i<s.size();i++) {\n // arr[i] = s[i]-'a'+1;\n // // cout<< arr[i] << endl;\n // }\n\n int sum = 0;\n\n for(int j=0;j<s.size();j++) {\n int ch = s[j] - 'a' + 1;\n auto [q, r] = div(ch, 10);\n sum = sum + q + r;\n }\n\n if (k == 1) return sum;\n k--;\n // cout << sum << endl;\n\n for(int x=sum; k>0 && sum >= 10; k--) {\n for(sum=0; x>0;) {\n auto [q, r] = div(x, 10);\n sum += r;\n x = q;\n }\n x = sum;\n }\n\n return sum;\n }\n};", "memory": "8300" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n long long transform2Sum(long long num){\n string trans1_string = std::to_string(num);\n long long start = pow(10,trans1_string.length()-1);\n long long trans2_int = 0;\n for(int i = 0; i < trans1_string.length()-1; i++){\n if(i < trans1_string.length() -2){\n trans2_int += (int) num/start;\n num = num % start;\n start /= 10;\n } else {\n trans2_int += (int) num/start;\n trans2_int += num%start;\n }\n }\n return trans2_int;\n }\n\n int getLucky(string s, int k) {\n long long trans1_int = 0;\n for(int i = 0; i < s.length(); i++){\n int num = (s[i] - 'a') + 1;\n trans1_int += (((int) num/10) + num%10);\n }\n\n for(int j = 0; j < k-1; j++){\n trans1_int = transform2Sum(trans1_int);\n if(trans1_int < 10) break;\n }\n \n return trans1_int;\n }\n};", "memory": "8400" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n vector<int>v;\n int val=0;\n for(int i=1;i<27;i++)\n {\n v.push_back(i);\n }\n string s1=\"\";\n for(int i=0;i<s.length();i++)\n {\n if(v[s[i]-'a']>9)\n s1+=char(v[s[i]-'a']/10+'0');\n s1+=char(v[s[i]-'a']%10+'0');\n }\n string s2=\"\";\n while(k>0)\n {\n val=0;\n for(int i=0;i<s1.length();i++)\n {\n val+=s1[i]-'0';\n }\n s2=\"\";\n while(val>0)\n {\n s2+=char(val%10+'0');\n val/=10;\n }\n s1=s2;\n cout<<s1<<endl;\n k--;\n }\n if(!k%2)\n reverse(s1.begin(),s1.end());\n cout<<s1;\n val=stoi(s1);\n return val;\n }\n};", "memory": "8500" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int sum = 0;\n string curr = s, new_curr;\n for(char c : curr) {\n int n = c - 'a' + 1;\n if(n / 10)\n new_curr += '0' + n / 10;\n new_curr += '0' + n % 10;\n }\n curr = new_curr;\n\n while (k--) {\n new_curr = \"\";\n sum = 0;\n for(char c : curr) {\n sum += c - '0';\n }\n int n = sum;\n while(n) {\n char c = '0' + n % 10;\n new_curr = c + new_curr;\n n /= 10;\n }\n curr = new_curr;\n }\n return sum;\n }\n};", "memory": "8600" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n stringstream ss_digits;\n for (const auto &c: s) ss_digits << c - 'a' + 1;\n \n string digits = ss_digits.str();\n int sum = 0;\n do {\n sum = 0;\n for (const auto &c: digits) sum += c - '0';\n digits = to_string(sum);\n } while (--k);\n\n return sum;\n }\n};", "memory": "9300" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n #include <unordered_map>\n\nunordered_map<char, int> m{\n {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, {'f', 6}, {'g', 7}, {'h', 8}, {'i', 9},\n {'j', 1}, {'k', 2}, {'l', 3}, {'m', 4}, {'n', 5}, {'o', 6}, {'p', 7}, {'q', 8}, {'r', 9},\n {'s', 10}, {'t', 2}, {'u', 3}, {'v', 4}, {'w', 5}, {'x', 6}, {'y', 7}, {'z', 8}\n};\n\n\n \n int sum=0;\n for(int i=0;i<s.size();i++){\n sum+=m[s[i]];\n }\n cout<<sum;\n k=k-1;\n while(k--){\n int x=sum;\n int add=0;\n while(x>0){\n add+=(x%10);\n x/=10;\n }\n sum=add; \n }\n return sum;\n }\n};", "memory": "9400" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n string alphabet = \"abcdefghijklmnopqrstuvwxyz\";\n unordered_map<char,int>umap;\n for(int i=0;i<alphabet.length();i++)\n {\n umap[alphabet[i]]=i+1;\n }\n \n int sum=0;\n for(int i=0;i<s.length();i++)\n {\n int x = umap[s[i]];\n if(x>9)\n {\n sum+=x/10; // e.g 23 => 23/10 = 2\n sum+=x%10; // e.g 23 => 23%10 = 3\n }\n else{\n sum+=x;\n }\n }\n cout<<sum<<endl;\n k--;\n while(k>0)\n {\n int x = sum;\n sum = 0;\n while(x>0)\n {\n sum += x%10;\n x = x/10;\n }\n cout<<sum<<endl;\n k--;\n }\n return sum; \n }\n};", "memory": "9500" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string intToString(int num) {\n string result = \"\";\n if (num == 0) {\n return \"0\";\n }\n while (num > 0) {\n int digit = num % 10; \n result = char(digit + '0') + result; \n num /= 10; \n }\n return result;\n }\n int stringToInt(string str) {\n int result = 0;\n for (int i = 0; i < str.size(); i++) {\n result = result * 10 + (str[i] - '0'); \n }\n return result;\n }\n int getLucky(string s, int k) {\n string sum = \"\";\n string arr[26] = {\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \n \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \n \"23\", \"24\", \"25\", \"26\"};\n \n for (int i = 0; i < s.size(); i++) {\n char ch = s[i]; \n sum += arr[ch - 'a']; \n }\n for (int iter = 0; iter < k; iter++) {\n int add = 0; \n for (int i = 0; i < sum.size(); i++) {\n add += (sum[i] - '0'); \n }\n sum = intToString(add); \n }\n return stringToInt(sum); \n }\n};\n", "memory": "9600" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int n=s.length();\n int sum=0;\n string t=to_string(s[0]-96);\n for(int i=1;i<n;i++){\n t.append(to_string(s[i]-96));\n }\n cout<<t<<endl;\n for(int i=0;i<t.length();i++){\n sum+=int(t[i]-'0');\n }\n cout<<sum<<endl;\n for(int i=1;i<k;i++){\n int num=sum;\n sum=0;\n while(num>0){\n sum+=num%10;\n num=num/10;\n }\n // return dightalSum;\n }\n return sum;\n }\n};", "memory": "9700" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n int transform(int number){\n int sum=0;\n while(number>0){\n int digit= number%10;\n sum+= digit;\n number=number/10;\n }\n\n return sum;\n }\n int getLucky(string s, int k) {\n string str;\n for( auto& c: s){\n str += to_string((c-'a')+1);\n }\n int sum=0;\n for( auto& num:str){\n sum +=num-'0';\n }\n for(int i=1;i<k;i++){\n sum=transform(sum);\n }\n return sum;\n }\n};", "memory": "9800" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n string ans = \"\";\n int ret;\n for(auto c: s){\n ans += to_string(c-'a'+1);\n }\n \n for(int i = 1; i <= k; i++){\n ret = 0;\n for(auto c: ans){\n ret += c - '0';\n }\n ans = to_string(ret);\n }\n\n return ret;\n }\n};", "memory": "9800" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n\n\n\n string str;\n\n for(char c : s) {\n if(isalpha(c)) { \n \n int val = c - 'a' + 1;\n str += to_string(val); \n }\n }\n int ans = 0;\n while (k-- > 0) {\n ans = 0; \n for (char digit : str) {\n ans += digit - '0'; \n }\n str = to_string(ans); \n }\n\n return ans;\n }\n};", "memory": "9900" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int n=s.size();\n string arr;\n for(int i=0;i<n;i++){\n int num=s[i]-'a'+1;\n arr+=to_string(num);\n }\n while(k--){\n long long sum=0;\n for(int i=0;i<arr.size();i++){\n sum+=arr[i]-'0';\n }\n arr=to_string(sum);\n }\n return stoi(arr);\n } \n};", "memory": "9900" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getCharVal(char s)\n {\n return s-'a'+1;\n }\n string transform(string &s)\n {\n string ts= \"\";\n for(int i=0;i<s.size();i++)\n {\n ts+=to_string(getCharVal(s[i]));\n }\n return ts;\n } \n int addAllNumber(string &s)\n {\n int sum=0;\n for(int i=0;i<s.size();i++)\n {\n sum+=s[i]-'0';\n }\n return sum;\n }\n int getLucky(string s, int k) {\n string numString=transform(s);\n int ans=0;\n while(k>0)\n {\n ans=addAllNumber(numString);\n numString=to_string(ans);\n k--;\n }\n return ans;\n }\n};", "memory": "10000" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k)\n {\n std::string number; \n for(char c: s)\n {\n int n = c -'a' + 1;\n number.append(std::to_string(n));\n }\n std::cout<<number<<std::endl;\n\n while (--k >= 0)\n {\n int sum = 0;\n for(char c: number)\n {\n sum += c - '0';\n }\n number = std::to_string(sum);\n }\n\n return std::stoi(number);\n }\n};", "memory": "10000" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int len = s.length();\n string str;\n\n for(auto ch: s) {\n str += to_string(int(ch) - 96);\n }\n\n while(k--) {\n int sum = 0;\n for(auto ch: str)\n sum += int(ch) - 48;\n str = to_string(sum);\n }\n\n return stoi(str);\n }\n};", "memory": "10100" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n string st;\n\n for(int i=0; i<s.size(); i++){\n int num = s[i] - 'a' + 1;\n st.insert(st.size(), to_string(num));\n }\n\n for(int i=0; i<k; i++){\n int sum = 0;\n for(int j=0; j<st.size(); j++){\n sum += st[j] - '0';\n }\n st = to_string(sum);\n }\n\n return stoi(st);\n }\n};", "memory": "10100" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n // Convert each character in the string to its corresponding numeric value\n string number = \"\";\n for (char x : s) {\n number += to_string(x - 'a' + 1);\n \n }\n \n // Perform the transformation `k` times\n while (k > 0) {\n int temp = 0;\n for (char x : number) {\n temp += x - '0'; // Sum the digits of the current number\n }\n number = to_string(temp); // Convert the sum back to a string\n k--;\n }\n return stoi(number); // Return the final result as an integer\n }\n};", "memory": "10200" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n string number=\"\";\n for(char c:s){\n number+=to_string(1+c-'a');\n }\n while(k>0){\n int temp=0;\n for(char c:number){\n temp+=c-'0';\n }\n number=to_string(temp);\n k--;\n }\n return stoi(number);\n }\n};", "memory": "10200" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int calculate(string s)\n {\n int sum=0;\n\n for(int i=0;i<s.length();i++)\n {\n sum+=(int)s[i]-48;\n }\n return sum;\n }\n int getLucky(string s, int k) {\n int n=s.length();\n string sum=\"\";\n int num=0;\n for(int i=0;i<n;i++)\n {\n sum+=to_string(s[i]-'a'+1);\n }\n cout<< sum;\n while(k!=0)\n {\n num=calculate(sum);\n sum =to_string(num);\n k--;\n }\n return num;\n }\n};", "memory": "10300" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n long long string2Sum(string s){\n long long sum = 0;\n for(int i = 0; i < s.length(); i++){\n sum += s[i] - '0';\n }\n return sum;\n }\n\n int getLucky(string s, int k) {\n string transString = \"\";\n for(int i = 0; i < s.length(); i++){\n transString += std::to_string((s[i] - 'a') + 1);\n }\n\n int result;\n for(int j = 0; j < k; j++){\n result = string2Sum(transString);\n transString = std::to_string(result);\n }\n \n return result;\n }\n};", "memory": "10300" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n \n string num = \"\";\n int sum = 0; \n\n for(auto i:s){\n int pos = i - 'a' + 1;\n num += to_string(pos);\n }\n\n while(k--){\n string number = num;\n sum = 0;\n\n for(auto i:number){\n sum += (i - '0');\n }\n num = to_string(sum);\n }\n \n return sum;\n }\n};", "memory": "10400" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n string converted = \"\";\n int value = 0;\n\n if (!isdigit(s[0]))\n for (auto& chr : s)\n converted+= to_string(chr-96);\n else\n converted = s;\n \n\n for (auto& chr : converted)\n value += chr-48 ;\n \n\n if (k <= 1)\n return value;\n\n return getLucky(to_string(value),k-1);\n }\n};", "memory": "10500" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int ans = 0;\n vector<pair<char, int>> alphabetPairs = {\n {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, \n {'f', 6}, {'g', 7}, {'h', 8}, {'i', 9}, {'j', 10}, \n {'k', 11}, {'l', 12}, {'m', 13}, {'n', 14}, {'o', 15}, \n {'p', 16}, {'q', 17}, {'r', 18}, {'s', 19}, {'t', 20}, \n {'u', 21}, {'v', 22}, {'w', 23}, {'x', 24}, {'y', 25}, \n {'z', 26}\n };\n string appended = \"\";\n for (char ch : s) {\n for (auto& pair : alphabetPairs) {\n if (pair.first == ch) {\n appended += to_string(pair.second); \n break;\n }\n }\n }\n string next = appended;\n int sum = 0;\n while (k != 0) {\n sum = 0;\n for (int i = 0; i < next.size(); i++) {\n sum += (next[i] - '0'); \n }\n k--; \n if (k > 0) {\n next = to_string(sum);\n }\n }\n return sum;\n }\n};\n", "memory": "10600" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n int stringSize = s.size();\n int result = 0;\n //created a hashmap of char and its respective count;\n unordered_map<char, int> hashmap;\n\n for (char ch = 'a'; ch <= 'z'; ++ch) {\n hashmap[ch] = ch - 'a' + 1; // Calculate the count based on character position\n }\n \n string newString = \"\";\n for(char ch : s){\n newString+= to_string(hashmap[ch]);\n }\n\n while(k>0){\n result = sum(newString);\n newString = to_string(result);\n k--;\n }\n //cout<<newString;\n return result;\n }\n\n int sum(string s) {\n int newSum = 0; // Reset newSum for each function call\n for (char ch : s) {\n newSum += ch - '0'; // Convert each char digit to its integer form and sum them\n }\n return newSum;\n }\n};", "memory": "10700" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n unordered_map<char, int> alphabet = {\n {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, {'f', 6},\n {'g', 7}, {'h', 8}, {'i', 9}, {'j', 10}, {'k', 11}, {'l', 12},\n {'m', 13}, {'n', 14}, {'o', 15}, {'p', 16}, {'q', 17}, {'r', 18},\n {'s', 19}, {'t', 20}, {'u', 21}, {'v', 22}, {'w', 23}, {'x', 24},\n {'y', 25}, {'z', 26}};\n\n // Step 1: Convert each letter to its corresponding number string\n string numStr;\n for (char c : s) {\n numStr += to_string(alphabet[c]); // Append the number as a string\n }\n\n // Step 2: Perform the sum of digits transformation `k` times\n int num = 0;\n for (char digit : numStr) {\n num += digit - '0'; // Convert character to its digit value and sum it\n }\n\n // Perform the digit-sum transformation `k-1` more times\n k--; // We already did 1 transformation\n while (k-- > 0 && num >= 10) {\n int sum = 0;\n while (num > 0) {\n sum += num % 10; // Sum the digits\n num /= 10;\n }\n num = sum;\n }\n\n return num;\n }\n};\n", "memory": "10800" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int processNum(string s, int k) {\n if (k == 0) {\n return stoi(s);\n }\n\n int num = 0;\n for (int i = 0; i < s.size(); i++) {\n num += s[i] - '0';\n }\n\n if (k > 0) {\n return processNum(to_string(num), k - 1);\n }\n \n return num;\n }\n\n int getLucky(string s, int k) {\n if (k == 0) {\n return stoi(s);\n }\n\n long long num = 0;\n\n for (int i = 0; i < s.size(); i++) {\n num += processNum(to_string(s[i] - 'a' + 1), 1);\n }\n\n if (k > 0) {\n return processNum(to_string(num), k - 1);\n }\n\n return num;\n }\n};", "memory": "10900" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nint getLucky(string s, int k) {\n string convert = \"\";\n int result = 0;\n for(int i = 0; i < s.size(); i++) {\n convert += to_string(stoi(to_string(s.at(i))) - 97 + 1);\n }\n int j = 0;\n while(j < k) {\n result = 0;\n for(int L = 0; L < convert.size(); L++) {\n result += (stoi(to_string(convert.at(L))) - 48);\n // cout << result << endl;\n }\n\n if(k == 1) {\n return result;\n } else {\n convert = to_string(result);\n }\n j++;\n }\n return result;\n}\n};", "memory": "11000" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getLucky(string s, int k) {\n long long int num=0;\n string str=\"\";\n for(auto it: s)\n {\n str= str+ to_string(it-'a'+1);\n }\n cout<<str<<endl;\n \n while(k--)\n {\n int s=0;\n for(auto it:str)\n {\n s+=it-'0';\n }\n num=s;\n str=to_string(s);\n }\n return num ;\n\n }\n};", "memory": "11100" }
2,076
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <strong>convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e., replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code><strong> times</strong> in total.</p> <p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; ➝ &quot;(26)(2)(1)(24)&quot; ➝ &quot;262124&quot; ➝ 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li> <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;iiii&quot;, k = 1 <strong>Output:</strong> 36 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;iiii&quot; ➝ &quot;(9)(9)(9)(9)&quot; ➝ &quot;9999&quot; ➝ 9999 - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36 Thus the resulting integer is 36. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;leetcode&quot;, k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The operations are as follows: - Convert: &quot;leetcode&quot; ➝ &quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot; ➝ &quot;12552031545&quot; ➝ 12552031545 - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33 - Transform #2: 33 ➝ 3 + 3 ➝ 6 Thus the resulting integer is 6. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;zbax&quot;, k = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int processNum(string s, int k) {\n if (k == 0) {\n return stoi(s);\n }\n\n int num = 0;\n for (int i = 0; i < s.size(); i++) {\n num += s[i] - '0';\n }\n\n if (k > 0) {\n return processNum(to_string(num), k - 1);\n }\n \n return num;\n }\n\n int getLucky(string s, int k) {\n if (k == 0) {\n return stoi(s);\n }\n\n long long num = 0;\n\n for (int i = 0; i < s.size(); i++) {\n int numLocal = s[i] - 'a' + 1;\n if(numLocal >= 10) {\n num += processNum(to_string(numLocal), 1);\n } else {\n num += numLocal;\n }\n }\n\n if (k > 0) {\n return processNum(to_string(num), k - 1);\n }\n\n return num;\n }\n};", "memory": "11100" }