CoolFace
Apppublic

premaram/Programming_Challenges_Hub

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
intermediate_questions.cpython-310.pyc613 linesDownload Raw Back to __pycache__
1o

2˽g��@slddlZddlZddlZdd�Zdd�Zdd�Zdd	�Zd3d�Zdd
�Z	dd�Z4dd�Zdd�Zdd�Z
dS)�Nc
Cs�t�d�t�d�t�d�\}}|�}t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d5�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYWd�n1s�wY|��It�d�tj	dddd�}ddd�ddd�g}t6|�D]A\}}tjd|d��dd��)t�d|d�d��t�d|d�d��t�d |d!�d��Wd�n1s�wYq�t�d"��r�t�
�}z�t�|���i}t||�d#|v�r�|d#}d$}	t7|�D]Y\}}|d}8|d!}z*||9�}||k�rBt�d%|d�d&��nd}	t�d%|d�d'|�d(|���W�q t�yy}
zd}	t�d%|d�d)|
���WYd}
~
�q d}
~
ww|	�r�t�d*�nt�d+�nt�d,�Wd�n	1�s�wYWnt�y�}
zt�d-|
���WYd}
~
nd}
~
ww|��}|�r�t�d.�t�|�Wd�dSWd�dSWd�dS1�s�wYdS)/Nz%2243. Calculate Digit Sum of a Stringzu[Visit Leetcode](https://leetcode.com/problems/calculate-digit-sum-of-a-string/description/) for a better experience.�a�	10            **Problem**: You are given a string `s` consisting of digits and an integer `k`.11            12            A round can be completed if the length of `s` is greater than `k`. In one round, do the following:13            14            1. Divide `s` into consecutive groups of size `k` such that the first `k` characters are in the first group, 15               the next `k` characters are in the second group, and so on. Note that the size of the last group can 16               be smaller than `k`.17            2. Replace each group of `s` with a string representing the sum of all its digits. 18               For example, `"346"` is replaced with `"13"` because `3 + 4 + 6 = 13`.19            3. Merge consecutive groups together to form a new string. If the length of the string is greater than `k`, 20               repeat from step 1.21 22            Return `s` after all rounds have been completed.23 24            **Example 1**:25            - Input: `s = "11111222223", k = 3`26            - Output: `"135"`27            - Explanation: 28                - For the first round, we divide `s` into groups of size 3: `"111"`, `"112"`, `"222"`, and `"23"`.29                - Then we calculate the digit sum of each group: 30                    - `1 + 1 + 1 = 3`, 31                    - `1 + 1 + 2 = 4`, 32                    - `2 + 2 + 2 = 6`, 33                    - `2 + 3 = 5`. 34                - So, `s` becomes `"3" + "4" + "6" + "5" = "3465"` after the first round.35                - For the second round, we divide `s` into `"346"` and `"5"`.36                - Then we calculate the digit sum of each group: 37                    - `3 + 4 + 6 = 13`, 38                    - `5 = 5`. 39                - So, `s` becomes `"13" + "5" = "135"` after the second round. 40                - Now, `s.length <= k`, so we return `"135"` as the answer.41 42            **Example 2**:43            - Input: `s = "00000000", k = 3`44            - Output: `"000"`45            - Explanation: 46                - We divide `s` into `"000"`, `"000"`, and `"00"`.47                - Then we calculate the digit sum of each group: 48                    - `0 + 0 + 0 = 0`, 49                    - `0 + 0 + 0 = 0`, 50                    - `0 + 0 = 0`. 51                - `s` becomes `"0" + "0" + "0" = "000"`, whose length is equal to `k`, so we return `"000"`.52 53            **Constraints**:54            - `1 <= s.length <= 100`55            - `2 <= k <= 100`56            - `s` consists of digits only.57        �TopicsF��expandedzL 58                - **String**59                - **Simulation**60            �Hintz;Try simulating the entire process to find the final answer.�<Sample Answer (NOTE: every question has different solutions)a�61 62def digitSum(s, k):63        def divideString(s, k):64            l, n = [], len(s)65            for i in range(0, n, k):66                l.append(s[i:min(i + k, n)])67            return l68        while len(s)>k:69            arr, temp = divideString(s, k), [] 70            for group in arr: 71                group_sum = 072                for digit in group:73                    group_sum += int(digit)74                temp.append(str(group_sum)) 75            s = ''.join(temp) 76        return s77            �Flowchart of Sample Answerzimages\intermediate\1.png�Solve the Problem� Write your Python function here:�,z878def digitSum(s, k):79    # Your code goes here80    pass81��height�value)�11111222223��135��input�expected)�00000000r�000�View Test Case ��**Test Case �:**�82- Input: `r�`�- Expected Output: `r�Run Code�digitSumT�83Test case � passed!� failed: Expected �84, but got � raised an error: �All test cases passed!�0Some test cases failed. Please review your code.zGFunction `digitSum` not defined. Please define the function to proceed.�Error executing code: �Execution Output:��st�title�markdown�columns�write�expander�code�image�header�	text_area�	enumerate�button�io�StringIO�85contextlib�redirect_stdout�exec�	Exception�success�error�getvalue�	subheader�text)�col1�col2�86code_input�87test_cases�i�case�buffer�exec_globalsr�88all_passed�	input_val�expected_val�result�e�output�rO�NC:\Users\91939\OneDrive\Desktop\leetcode app\my_code\intermediate_questions.py�Q1s�8990913������V92����939495 �(��96�����97��E$�rQc
Cs t�d�t�d�t�d�\}}|�}t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d98�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYWd�n1s�wY|��jt�d�tj	dddd�}gd�dfgd�d�gd�dfgd�d�gd�dfgd�d�gd�dfgd�d�g}t99|�D]B\}}tjd|d ��dd��)t�d!|d �d"��t�d#|d$�d%��t�d&|d'�d%��Wd�n	1�swYq�t�d(��r�t�
�}z�t�|���i}t||�d)|v�r�|d)}d*}	t100|�D]Y\}}|d$}101|d'}z*||102�}||k�rct�d+|d �d,��nd}	t�d+|d �d-|�d.|���W�qAt�y�}
zd}	t�d+|d �d/|
���WYd}
~
�qAd}
~
ww|	�r�t�d0�nt�d1�nt�d2�Wd�n	1�s�wYWnt�y�}
zt�d3|
���WYd}
~
nd}
~
ww|��}|�r�t�d4�t�|�Wd�dSWd�dSWd�dS1�s	wYdS)5Nz1470. Shuffle the Arrayz[[Visit Leetcode](https://leetcode.com/problems/shuffle-the-array/) for a better experience.ra103            **Problem**: Given the array `nums` consisting of `2n` elements in the form 104            `[x1,x2,...,xn,y1,y2,...,yn]`, return the array in the form `[x1,y1,x2,y2,...,xn,yn]`.105 106            **Example 1**:107            - Input: `nums = [2,5,1,3,4,7], n = 3`108            - Output: `[2,3,5,4,1,7]` 109            - Explanation: Since `x1=2`, `x2=5`, `x3=1`, `y1=3`, `y2=4`, `y3=7`, then the answer is `[2,3,5,4,1,7]`.110 111            **Example 2**:112            - Input: `nums = [1,2,3,4,4,3,2,1], n = 4`113            - Output: `[1,4,2,3,3,2,4,1]`114 115            **Example 3**:116            - Input: `nums = [1,1,2,2], n = 2`117            - Output: `[1,2,1,2]`118 119            **Constraints**:120            - `1 <= n <= 500`121            - `nums.length == 2n`122            - `1 <= nums[i] <= 10^3123        rFrz* 124                - **Array**125            rz�Use two pointers to create the new array of 2n elements. The first starting at the beginning and the other starting at (n+1)th position. Alternate between them and create the new array.ra{126                def shuffle(nums, n):127                    left = 0128                    right = n   129                    ans = []130 131                    while right < len(nums):132                        ans.append(nums[left])133                        ans.append(nums[right])134                        left+=1135                        right+=1136                    return ans137            rzimages\intermediate\2.pngr	r138rz;139def shuffle(nums, n) :140    # Your code goes here141    pass142r)r�rr��r)rrrRrSrrTr)rrrrSrSrrrrS)rrSrrrrrSr)rrrr)rrrr)�143���(�2�<)rUrXrVrYrWrZrrrrrrrrrr�shuffleTr r!r"r#r$r%r&zFFunction `shuffle` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr[rIrJrKrLrMrNrOrOrP�Q2�s�144145146������4147����148149150 �(��151�����152��G$�r\c
Cst�d�t�d�t�d�\}}|�}t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d153�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYWd�n1s�wY|��`t�d�tj	dddd�}gd�dfgd�d�gd�dfgd�d�gd�dfgd�d�g}t154|�D]B\}}tjd|d��dd��)t�d|d�d ��t�d!|d"�d#��t�d$|d%�d#��Wd�n	1�s155wYq�t�d&��r�t�
�}z�t�|���i}t||�d'|v�r�|d'}d(}	t156|�D]Y\}}|d"}157|d%}z*||158�}||k�rYt�d)|d�d*��nd}	t�d)|d�d+|�d,|���W�q7t�y�}
zd}	t�d)|d�d-|
���WYd}
~
�q7d}
~
ww|	�r�t�d.�nt�d/�nt�d0�Wd�n	1�s�wYWnt�y�}
zt�d1|
���WYd}
~
nd}
~
ww|��}|�r�t�d2�t�|�Wd�dSWd�dSWd�dS1�s�wYdS)3Nz.1431. Kids With the Greatest Number of Candiesz~[Visit Leetcode](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/) for a better experience.ra^159            **Problem**: There are n kids with candies. You are given an integer array `candies`, 160            where `candies[i]` represents the number of candies the ith kid has, and an integer `extraCandies`, 161            denoting the number of extra candies that you have.162 163            Return a boolean array `result` of length n, where `result[i]` is true if, after giving the ith kid all 164            the `extraCandies`, they will have the greatest number of candies among all the kids, or false otherwise.165 166            **Example 1**:167            - Input: `candies = [2,3,5,1,3], extraCandies = 3`168            - Output: `[true,true,true,false,true]` 169 170            **Example 2**:171            - Input: `candies = [4,2,1,1,2], extraCandies = 1`172            - Output: `[true,false,false,false,false]` 173 174            **Example 3**:175            - Input: `candies = [12,1,12], extraCandies = 10`176            - Output: `[true,false,true]`177 178            **Constraints**:179            - `n == candies.length`180            - `2 <= n <= 100`181            - `1 <= candies[i] <= 100`182            - `1 <= extraCandies <= 50183        rFrz+ 184                - **Array**185 186            ruJFor each kid check if candies[i] + extraCandies ≥ maximum in Candies[i].ra,187            def kidsWithCandies(candies, extraCandies):188                max_val = max(candies) 189                result = [] 190 191                for i in range(len(candies)):192                    result.append(candies[i] + extraCandies >= max_val)193 194                return result195                        rzimages\intermediate\3.pngr	r196rzP197def kidsWithCandies(candies, extraCandies):198    # Your code goes here199    pass200r)rrrRrrr)TTTFTr)rSrrrrr)TFFFF)�rr]rU)TFTrrrrrrrrr�kidsWithCandiesTr r!r"r#r$r%r&zNFunction `kidsWithCandies` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr^rIrJrKrLrMrNrOrOrP�Q39s�201202203������5204����205206207 �(��208�����209��F$�r_c
C��t�d�t�d�t�d�\}}|�}t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d210�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYWd�n1s�wY|��St�d�tj	dddd�}gd�dd�gd�dd�gd�dd�g}t211|�D]A\}}tjd|d��dd��)t�d|d�d��t�d|d �d!��t�d"|d#�d!��Wd�n1s�wYq�t�d$��r�t�
�}z�t�|���i}t||�d%|v�r�|d%}d&}	t212|�D]Y\}}|d }213|d#}z*||214�}||k�rLt�d'|d�d(��nd}	t�d'|d�d)|�d*|���W�q*t�y�}
zd}	t�d'|d�d+|
���WYd}
~
�q*d}
~
ww|	�r�t�d,�nt�d-�nt�d.�Wd�n	1�s�wYWnt�y�}
zt�d/|
���WYd}
~
nd}
~
ww|��}|�r�t�d0�t�|�Wd�dSWd�dSWd�dS1�s�wYdS)1Nz1512. Number of Good Pairszj[Visit Leetcode](https://leetcode.com/problems/number-of-good-pairs/description/) for a better experience.ra�215            **Problem**: Given an array of integers `nums`, return the number of good pairs.216 217            A pair (i, j) is called good if `nums[i] == nums[j]` and `i < j`.218 219            **Example 1**:220            - Input: `nums = [1,2,3,1,1,3]`221            - Output: `4` 222            - Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.223 224            **Example 2**:225            - Input: `nums = [1,1,1,1]`226            - Output: `6` 227            - Explanation: Each pair in the array are good.228 229            **Example 3**:230            - Input: `nums = [1,2,3]`231            - Output: `0` 232 233            **Constraints**:234            - `1 <= nums.length <= 100`235            - `1 <= nums[i] <= 100236        rFrz� 237                - **Array**238                - **Hash Table**239                - **Math**240                - **counting**241            ru�Count how many times each number appears. If a number appears n times, then n * (n – 1) // 2 good pairs can be made with this number.ra242from collections import Counter  243 244def numIdenticalPairs(nums):245    frequency = Counter(nums)  246    counter = 0 247    for count in frequency.values():248        if count > 1:249            counter += (count * (count - 1)) // 2  250    return counter251 252            rzimages\intermediate\4.pngr	r253rzA254def numIdenticalPairs(nums):255    # Your code goes here256    pass257r)rrrrrrrSr)rrrr��rrrrrrrrrrrrrr�numIdenticalPairsTr r!r"r#r$r%r&zPFunction `numIdenticalPairs` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHrcrIrJrKrLrMrNrOrOrP�Q4�s�258259260������7261����262263264 �(��265�����266��F$�rdc
Cs8t�d�t�d�t�d�\}}|��t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d267�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYtjddd��
t�d�Wd�n1s�wYWd�n1s�wY|��Zt�d�tj	dddd�}dgd�fdd�dgd�fdd�dgd�fdd�g}t268|�D]B\}}tjd|d ��dd��)t�d!|d �d"��t�d#|d$�d%��t�d&|d'�d%��Wd�n	1�s wYq�t�d(��rt�
�}z�t�|���i}t||�d)|v�r�|d)}d*}	t269|�D]Y\}}|d$}270|d'}z*||271�}||k�rot�d+|d �d,��nd}	t�d+|d �d-|�d.|���W�qMt�y�}
zd}	t�d+|d �d/|
���WYd}
~
�qMd}
~
ww|	�r�t�d0�nt�d1�nt�d2�Wd�n	1�s�wYWnt�y�}
zt�d3|
���WYd}
~
nd}
~
ww|��}|�r	t�d4�t�|�Wd�dSWd�dSWd�dS1�swYdS)5Nz,1684. Count the Number of Consistent Stringsz|[Visit Leetcode](https://leetcode.com/problems/count-the-number-of-consistent-strings/description/) for a better experience.ra�272            **Problem**: You are given a string `allowed` consisting of distinct characters and an array of strings `words`. 273            A string is consistent if all characters in the string appear in the string `allowed`.274 275            Return the number of consistent strings in the array `words`.276 277            **Example 1**:278            - Input: `allowed = "ab", words = ["ad","bd","aaab","baa","badab"]`279            - Output: `2` 280            - Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.281 282            **Example 2**:283            - Input: `allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]`284            - Output: `7` 285            - Explanation: All strings are consistent.286 287            **Example 3**:288            - Input: `allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]`289            - Output: `4` 290            - Explanation: Strings "cc", "acd", "ac", and "d" are consistent.291 292            **Constraints**:293            - `1 <= words.length <= 10^4`294            - `1 <= allowed.length <= 26`295            - `1 <= words[i].length <= 10`296            - The characters in `allowed` are distinct.297            - `words[i]` and `allowed` contain only lowercase English letters.298        rFrz� 299                - **Strings**300                - **Hash Table**301                - **Array**302                - **Bit Manipullation**303                - **Counting**  304            �Hint 1zEA string is incorrect if it contains a character that is not allowed.�Hint 2z-Constraints are small enough for brute force.ray305def countConsistentStrings(allowed, words):306        ans = 0307        allowed = set(allowed) 308        309        for word in words:  310            word = set(word) 311            flag = True312            for ch in word:313                if ch not in allowed: 314                    flag = False315                    break316            ans += flag  317        318        return ans319            rzimages\intermediate\5.pngr	r320rzP321def countConsistentStrings(allowed, words):322    # Your code goes here323    pass324r�ab)�ad�bd�aaab�baa�badabr�abc)�a�b�crg�ac�bcrmrT�cad)�cc�acdro�ba�bac�badrq�drSrrrrrrrrrr�countConsistentStringsTr r!r"r#r$r%r&zUFunction `countConsistentStrings` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHrzrIrJrKrLrMrNrOrOrP�Q5Ms�325326327�	������D328����329330331 �(��332�����333��F$�r{c
Cs�t�d�t�d�t�d�\}}|�}t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d334�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYWd�n1s�wY|��Mt�d�tj	dddd�}ddd�ddd�ddd�g}t335|�D]A\}}tjd|d��dd��)t�d|d�d��t�d|d�d��t�d |d!�d��Wd�n1s�wYq�t�d"��r�t�
�}z�t�|���i}t||�d#|v�r�|d#}d$}	t336|�D]Y\}}|d}337|d!}z*||338�}||k�rFt�d%|d�d&��nd}	t�d%|d�d'|�d(|���W�q$t�y}}
zd}	t�d%|d�d)|
���WYd}
~
�q$d}
~
ww|	�r�t�d*�nt�d+�nt�d,�Wd�n	1�s�wYWnt�y�}
zt�d-|
���WYd}
~
nd}
~
ww|��}|�r�t�d.�t�|�Wd�dSWd�dSWd�dS1�s�wYdS)/Nz.1614. Maximum Nesting Depth of the Parenthesesz~[Visit Leetcode](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/description/) for a better experience.ra�339            **Problem**: Given a valid parentheses string `s`, return the nesting depth of `s`. 340            The nesting depth is the maximum number of nested parentheses.341 342            **Example 1**:343            - Input: `s = "(1+(2*3)+((8)/4))+1"`344            - Output: `3`345            - Explanation: Digit 8 is inside of 3 nested parentheses in the string.346 347            **Example 2**:348            - Input: `s = "(1)+((2))+(((3)))"`349            - Output: `3`350            - Explanation: Digit 3 is inside of 3 nested parentheses in the string.351 352            **Example 3**:353            - Input: `s = "()(())((()()))"`354            - Output: `3`355 356            **Constraints**:357            - `1 <= s.length <= 100`358            - `s` consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.359            - It is guaranteed that parentheses expression `s` is a VPS (valid parentheses string).360        rFrzH 361                - **Strings**362                - **Stack**363            rz|The depth of any character in the VPS is the ( number of left brackets before it ) - ( number of right brackets before it ).rz�364def maxDepth(s):365    stk = []366    ans=0367    for x in s:368        if x=='(':369            stk.append(x)370        elif x==')' and stk and stk[-1] == '(':371            ans=max(ans,len(stk))372            stk.pop()373    return ans374            rzimages\intermediate\6.pngr	r375rz5376def maxDepth(s):377    # Your code goes here378    pass379rz(1+(2*3)+((8)/4))+1rrz(1)+((2))+(((3)))z()(())((()()))rrrrrrrrrr�maxDepthTr r!r"r#r$r%r&zGFunction `maxDepth` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr|rIrJrKrLrMrNrOrOrP�Q6�s�380381382������5383����384385386 �(��387�����388��F$�r}c
Cs�t�d�t�d�t�d�\}}|�}t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d389�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYWd�n1s�wY|��It�d�tj	dddd�}ddd�ddd�g}t390|�D]A\}}tjd|d��dd��)t�d|d�d��t�d|d�d��t�d|d �d��Wd�n1s�wYq�t�d!��r�t�
�}z�t�|���i}t||�d"|v�r�|d"}d}	t391|�D]Y\}}|d}392|d }z*||393�}||k�rBt�d#|d�d$��nd}	t�d#|d�d%|�d&|���W�q t�yy}
zd}	t�d#|d�d'|
���WYd}
~
�q d}
~
ww|	�r�t�d(�nt�d)�nt�d*�Wd�n	1�s�wYWnt�y�}
zt�d+|
���WYd}
~
nd}
~
ww|��}|�r�t�d,�t�|�Wd�dSWd�dSWd�dS1�s�wYdS)-Nz*1704. Determine if String Halves Are Alikezz[Visit Leetcode](https://leetcode.com/problems/determine-if-string-halves-are-alike/description/) for a better experience.ra�394            **Problem**: Given a string `s` of even length, split this string into two halves of equal lengths, 395            and let `a` be the first half and `b` be the second half.396 397            Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U').398            Return `true` if `a` and `b` are alike. Otherwise, return `false`.399 400            **Example 1**:401            - Input: `s = "book"`402            - Output: `true`403            - Explanation: `a = "bo"` and `b = "ok"`. `a` has 1 vowel and `b` has 1 vowel. Therefore, they are alike.404 405            **Example 2**:406            - Input: `s = "textbook"`407            - Output: `false`408            - Explanation: `a = "text"` and `b = "book"`. `a` has 1 vowel whereas `b` has 2. Therefore, they are not alike.409 410            **Constraints**:411            - `2 <= s.length <= 1000`412            - `s.length` is even.413            - `s` consists of uppercase and lowercase letters.414        rFrzK 415                - **Strings**416                - **Counting**417            rzWCreate a function that checks if a character is a vowel, either uppercase or lowercase.rz�418def halvesAreAlike(s):419    cnt, cnt2, ln = 0, 0, len(s)420    vowels = set('aeiouAEIOU')421    for i in range(ln//2):422        if s[i] in vowels: cnt += 1423        if s[i+ln//2] in vowels: cnt2 += 1424    return cnt == cnt2425            rzimages\intermediate\7.pngr	r426rz;427def halvesAreAlike(s):428    # Your code goes here429    pass430r�bookTr�textbookrrrrrrrrrr�halvesAreAliker r!r"r#r$r%r&zMFunction `halvesAreAlike` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr�rIrJrKrLrMrNrOrOrP�Q7os�431432433���434���0435����436437438 �(��439�����440��E$�r�c
Cr`)1Nz02114. Maximum Number of Words Found in Sentencesz�[Visit Leetcode](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/description/) for a better experience.ra�441            **Problem**: A sentence is a list of words that are separated by a single space with no leading or trailing spaces.442            You are given an array of strings `sentences`, where each `sentences[i]` represents a single sentence.443            444            Return the maximum number of words that appear in a single sentence.445 446            **Example 1**:447            - Input: `sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]`448            - Output: `6`449            - Explanation: The first sentence has 5 words, the second has 4, and the third has 6 words. The maximum is 6.450 451            **Example 2**:452            - Input: `sentences = ["please wait", "continue to fight", "continue to win"]`453            - Output: `3`454            - Explanation: The second and third sentences contain the same number of words, which is 3.455 456            **Constraints**:457            - `1 <= sentences.length <= 100`458            - `1 <= sentences[i].length <= 100`459            - `sentences[i]` consists only of lowercase English letters and ' ' only.460            - `sentences[i]` does not have leading or trailing spaces.461            - All the words in `sentences[i]` are separated by a single space.462        rFrzH 463                - **Strings**464                - **Array**465            rz�Process each sentence separately and count the number of words by looking for the number of space characters in the sentence and adding it by 1.rz�466def mostWords(sentences):467    m=0468    for i in sentences:469        c=0470        for j in i:471            if j == ' ':472                c+=1473        m=max(m,c+1)474    return m475            rzimages\intermediate\8.pngr	r476rz>477def mostWords(sentences):478    # Your code goes here479    pass480r)zalice and bob love leetcodezi think so toozthis is great thanks very muchrar)zplease waitzcontinue to fightzcontinue to winr)�onez	two threezfour five six sevenrSrrrrrrrrrr�	mostWordsTr r!r"r#r$r%r&zHFunction `mostWords` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr�rIrJrKrLrMrNrOrOrP�Q8�s�481482483������3484����485486487 �(��488�����489��F$�r�c
Cs0t�d�t�d�t�d�\}}|��t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d490�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYtjddd��
t�d�Wd�n1s�wYWd�n1s�wY|��Vt�d�tj	dddd�}ddd�ddd�ddd�ddd�ddd�g}t491|�D]B\}}tjd|d��dd��)t�d|d�d ��t�d!|d"�d#��t�d$|d%�d#��Wd�n	1�swYq�t�d&��r�t�
�}z�t�|���i}t||�d'|v�r�|d'}d}	t492|�D]Y\}}|d"}493|d%}z*||494�}||k�rkt�d(|d�d)��nd}	t�d(|d�d*|�d+|���W�qIt�y�}
zd}	t�d(|d�d,|
���WYd}
~
�qId}
~
ww|	�r�t�d-�nt�d.�nt�d/�Wd�n	1�s�wYWnt�y�}
zt�d0|
���WYd}
~
nd}
~
ww|��}|�rt�d1�t�|�Wd�dSWd�dSWd�dS1�swYdS)2Nz>1941. Check if All Characters Have Equal Number of Occurrencesz�[Visit Leetcode](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/description/) for a better experience.ra=495            **Problem**: Given a string `s`, return true if `s` is a good string, or false otherwise.496            497            A string `s` is good if all the characters that appear in `s` have the same number of occurrences.498 499            **Example 1**:500            - Input: `s = "abacbc"`501            - Output: `true`502            - Explanation: The characters that appear in `s` are 'a', 'b', and 'c'. All characters occur 2 times in `s`.503 504            **Example 2**:505            - Input: `s = "aaabb"`506            - Output: `false`507            - Explanation: The characters that appear in `s` are 'a' and 'b'. 'a' occurs 3 times while 'b' occurs 2 times, which is not the same number of times.508 509            **Constraints**:510            - `1 <= s.length <= 1000`511            - `s` consists of lowercase English letters.512        rFrzl 513                - **Strings**514                - **Hash Table**515                - **Counting**516            rezLBuild a dictionary containing the frequency of each character appearing in srfz3Check if all values in the dictionary are the same.ra~517 518def areOccurrencesEqual(s):519        d = {}520 521        for i in s:522            d[i] = d.get(i,0) + 1523 524        values = list(d.values())525 526        first_value = values[0]527        528        index = 0529        length = len(values)530 531        while index < length:532            if first_value != values[index]:533                return False534            index+=1535        return True536            rzimages\intermediate\9.pngr	r537rz@538def areOccurrencesEqual(s):539    # Your code goes here540    pass541r�abacbcTr�aaabb�abcabc�aabbccrmrrrrrrrrrr�areOccurrencesEqualr r!r"r#r$r%r&zRFunction `areOccurrencesEqual` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr�rIrJrKrLrMrNrOrOrP�Q9zs�542543544�������>545��	��546547548 �(��549�����550��H$�r�c
Csnt�d�t�d�t�d�\}}|��t�d�tjddd��
t�d�Wd�n1s0wYtjd	dd��
t�d551�Wd�n1sLwYtjddd��
t�d�Wd�n1shwYtjd
dd��
t�d�Wd�n1s�wYtjddd��
t�d�Wd�n1s�wYtjddd��
t�d�Wd�n1s�wYWd�n1s�wY|��Yt�d�tj	dddd�}gd�dd�gd�dd�dgdd�gd�dd�g}t552|�D]B\}}tjd |d!��dd��)t�d"|d!�d#��t�d$|d%�d&��t�d'|d(�d&��Wd�n	1�s;wYq�t�d)��rt�
�}z�t�|���i}t||�d*|v�r�|d*}d+}	t553|�D]Y\}}|d%}554|d(}z*||555�}||k�r�t�d,|d!�d-��nd}	t�d,|d!�d.|�d/|���W�qht�y�}
zd}	t�d,|d!�d0|
���WYd}
~
�qhd}
~
ww|	�r�t�d1�nt�d2�nt�d3�Wd�n	1�s�wYWnt�y}
zt�d4|
���WYd}
~
nd}
~
ww|��}|�r$t�d5�t�|�Wd�dSWd�dSWd�dS1�s0wYdS)6Nz51827. Minimum Operations to Make the Array Increasingz�[Visit Leetcode](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/description/) for a better experience.ra}556            **Problem**: You are given an integer array `nums` (0-indexed). In one operation, you can choose an element of the array and increment it by 1.557 558            Return the minimum number of operations needed to make `nums` strictly increasing.559 560            An array `nums` is strictly increasing if `nums[i] < nums[i+1]` for all `0 <= i < nums.length - 1`. An array of length 1 is trivially strictly increasing.561 562            **Example 1**:563            - Input: `nums = [1,1,1]`564            - Output: `3`565            - Explanation: Increment operations to make it strictly increasing.566 567            **Example 2**:568            - Input: `nums = [1,5,2,4,1]`569            - Output: `14`570 571            **Example 3**:572            - Input: `nums = [8]`573            - Output: `0`574 575            **Constraints**:576            - `1 <= nums.length <= 5000`577            - `1 <= nums[i] <= 10^4578        rFrzG 579                - **Array**580                - **Greedy**581            rez0nums[i+1] must be at least equal to nums[i] + 1.rfzFThink greedily. You don't have to increase nums[i+1] beyond nums[i]+1.zHint 3z9Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]).ra�582def minOperations(nums):583 584        n = len(nums)585        if n == 1:586            return 0  587        588        ans = 0589        index = 1  590 591        while index < n:592            if nums[index] <= nums[index - 1]:593                ans += nums[index - 1] - nums[index] + 1594                nums[index] = nums[index - 1] + 1595            596            index += 1  597        return ans598            rzimages\intermediate\10.pngr	r599rz=600def minOperations(nums):601    # Your code goes here602    pass603r)rrrrr)rrRrrSr��rrbrrrrrrrrrr�
minOperationsTr r!r"r#r$r%r&zLFunction `minOperations` not defined. Please define the function to proceed.r'r(r))rArBrCrDrErFrGrHr�rIrJrKrLrMrNrOrOrP�Q10s�604605606��������C607�608���609610611 �(��612�����613��G$�r�)�	streamlitr*r6r8rQr\r_rdr{r}r�r�r�r�rOrOrOrP�<module>s,-