Number of Atoms | Made Easy | Full Dry Run | Leetcode 726 | Google | codestorywithMIK
iPad PDF Notes - https://github.com/MAZHARMIK/Intervie...
Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 21st Video of our Playlist "Stack : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a very good Stack based problem : Number of Atoms | Made Easy | Full Dry Run | Leetcode 726 | Google | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Number of Atoms | Made Easy | Full Dry Run | Leetcode 726 | Google | codestorywithMIK
Company Tags : GOOGLE, APPLE
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Intervie...
Leetcode Link : https://leetcode.com/problems/number-...
My DP Concepts Playlist : • Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : • Graph Concepts & Qns - 1 : Graph will...
My Recursion Concepts Playlist : • Introduction | Recursion Concepts And...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Use a Stack to Handle Nested Structures:
A stack is used to manage the nested nature of chemical formulas. Each level of parentheses in the formula corresponds to a new map on the stack that tracks element counts within that scope.
Iterate Through the Formula:
As we iterate through the formula string, we encounter different characters:
'(': Push a new empty map onto the stack to start a new scope.
')': Pop the top map from the stack (representing the current scope), parse the subsequent number (if any) as the multiplier, and merge this scope's counts into the next map on the stack, adjusting counts by the multiplier.
Element Symbols and Counts: Parse element symbols (which can be a single uppercase letter followed by zero or more lowercase letters) and their counts (if any). Update the count of each element in the current top map of the stack.
Merge Maps and Adjust Counts:
When encountering a closing parenthesis, the map from the current scope is merged into the next map on the stack, with counts adjusted by any multiplier following the closing parenthesis.
Construct the Result:
After parsing the entire formula, the stack will contain one map representing the final count of each element. This map is sorted lexicographically (by element name) to construct the result string in the required format: each element followed by its count (if greater than one).
Return the Result:
The result string is built by iterating over the sorted map and concatenating each element and its count to the result string.
This method efficiently handles nested structures and ensures that all elements and their counts are accurately represented in the final result.
✨ Timelines✨
00:00 - Introduction
00:23 - Problem Explanation
04:59 - Why Stack ?
12:00 - Simplest Thought Process + Complete Dry Run
23:35 - Coding it up
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024