Leetcode with dani
Question: Count Substrings with Same Start and End Character Given a string s consisting only of lowercase English letters, your task is to find the number of substrings that start and end with the same character. ▎Examples: 1. Input: "abcba" Output:…
sorry for the minor error on the test cases , here is the updated
Question: Count Substrings with Same Start and End Character
Given a string s consisting only of lowercase English letters, your task is to find the number of substrings that start and end with the same character.
Examples:
Input: "abcba"
Output: 7
Explanation: The substrings are: "a", "b", "c", "b", "a", "bcb", and "aba".
Input: "abacada"
Output: 13
Explanation: The substrings are: "a", "b", "a", "c", "a", "d", "a", "aba", "aca", "ada", "abaca", "abada", and "abacad".
Input: "a"
Output: 1
Explanation: The only substring is "a".
Input: "zzzz"
Output: 10
Explanation: The substrings are: "z", "z", "z", "z", "zz", "zz", "zz", "zzz", "zzz", and "zzzz".
Challenge:
Write a function that takes a string as input and returns the total count of substrings that start and end with the same character.
Question: Count Substrings with Same Start and End Character
Given a string s consisting only of lowercase English letters, your task is to find the number of substrings that start and end with the same character.
Examples:
Input: "abcba"
Output: 7
Explanation: The substrings are: "a", "b", "c", "b", "a", "bcb", and "aba".
Input: "abacada"
Output: 13
Explanation: The substrings are: "a", "b", "a", "c", "a", "d", "a", "aba", "aca", "ada", "abaca", "abada", and "abacad".
Input: "a"
Output: 1
Explanation: The only substring is "a".
Input: "zzzz"
Output: 10
Explanation: The substrings are: "z", "z", "z", "z", "zz", "zz", "zz", "zzz", "zzz", and "zzzz".
Challenge:
Write a function that takes a string as input and returns the total count of substrings that start and end with the same character.
👍10
Another Interview Question
▎Problem Statement
You are given a string s consisting only of the letters 'a' and 'b', and an integer k. You need to find the minimum number of character changes needed to obtain a substring of length ≥ k where all characters are the same.
Test Cases
Here are some test cases to illustrate the problem:
▎Test Case 1
• Input: s = "aabbaa", k = 3
• Output: 1
• Explanation: The substring "aaa" can be formed by changing one 'b' to 'a'.
▎Test Case 2
• Input: s = "ababab", k = 4
• Output: 2
• Explanation: To form "aaaa" or "bbbb", you need to change two characters (e.g., "aaab" or "bbba").
▎Test Case 3
• Input: s = "aaaaaa", k = 2
• Output: 0
▎Problem Statement
You are given a string s consisting only of the letters 'a' and 'b', and an integer k. You need to find the minimum number of character changes needed to obtain a substring of length ≥ k where all characters are the same.
Test Cases
Here are some test cases to illustrate the problem:
▎Test Case 1
• Input: s = "aabbaa", k = 3
• Output: 1
• Explanation: The substring "aaa" can be formed by changing one 'b' to 'a'.
▎Test Case 2
• Input: s = "ababab", k = 4
• Output: 2
• Explanation: To form "aaaa" or "bbbb", you need to change two characters (e.g., "aaab" or "bbba").
▎Test Case 3
• Input: s = "aaaaaa", k = 2
• Output: 0
👍7
Forwarded from Eunuch
Report on my Interview
Literally when I started my PC stacked🤯 and he was a peace guy he literally gave me 20 min after that I changed my PC with my friend and shared that.
1. After I shared my PC he introduced himself to me and also told me to share my screen asked me to go from tab desktop to check whether I shared my screen.
Also he asked me to show my ID in front of the screen.
2. He dropped the question it was
leetcode 1198 Premium question (https://github.com/doocs/leetcode/blob/main/solution/1100-1199/1198.Find%20Smallest%20Common%20Element%20in%20All%20Rows/README_EN.md)
3. I asked him to give me some time to understand the problem. When I saw the word strictly increasing I thought the question was Monotonic Stack but it wasn't.
4. After few silent minutes I think my adrelanine hormone just increased and the optimal one came to me but not the brute.
5. So I thought of a way to just sort the dictionary and make explain it as brute force. And literally it worked and asked me to Optimize it after I explained the time complexity. He also guided me on some other optimal related things like he told me to finish it inside the nested loop since it is strictly increasing matrix so I literally did that and explained as Optimal form and after I explained I implemented it quickly I started implementing while I was explaining the Brute one.
6. The technical was like that we finished it in 30min after that he gave 3 min and copy the code run and submit. He didn't told me whether it was accepted but I personally tested it on sharepad and it was correct for th given testcases.
7. After 5 min we started the Behavioural Interview he asked me 7 questions and agree on the policy the questions was literally same as the
G4 Interview Template
so I highly recommend to prepare on it.
8. After the interview I asked him about A2SV and other tech staff since he was Graduate student. And the interview entirely ended in 1:10 hr.
👍10
"""
Given a binary array data, return the minimum number of swaps required to group all 1’s present in the array together in any place in the array.
Example 1:
Input: data = [1,0,1,0,1]
Output: 1
Explanation: There are 3 ways to group all 1's together:
[1,1,1,0,0] using 1 swap.
[0,1,1,1,0] using 2 swaps.
[0,0,1,1,1] using 1 swap.
The minimum is 1.
Example 2:
Input: data = [0,0,0,1,0]
Output: 0
Explanation: Since there is only one 1 in the array, no swaps are needed.
Example 3:
Input: data = [1,0,1,0,1,0,0,1,1,0,1]
Output: 3
Explanation: One possible solution that uses 3 swaps is [0,0,0,0,0,1,1,1,1,1,1].
Constraints:
1 <= data.length <= 10^5
data[i] is either 0 or 1.
"""
👍4
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized.
Return the maximized sum.
Example 1:
Input: nums = [1,4,3,2]
Output: 4
Explanation: All possible pairings (ignoring the ordering of elements) are:
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
So the maximum possible sum is 4.
Example 2:
Input: nums = [6,2,6,5,1,2]
Output: 9
Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
1 2 2 5 6 6
Constraints:
1 <= n <= 104
nums.length == 2 * n
-104 <= nums[i] <= 104
I am also practicing for the A2SV interview, so please wish me good luck😁
🙏16❤4
Leetcode with dani pinned «https://news.1rj.ru/str/+m835nLa3A0c2MjBk join our discussion group»
Join our group and let's build a community of tech friends. The best thing about networking is that you might meet someone who shares a job opportunity with you, helping you land your first job. That friend could support you on your tech journey, or you could be that friend for someone else. I also want to be part of this group, so feel free to connect with me. Please Share to ur friends
https://news.1rj.ru/str/+m835nLa3A0c2MjBk
https://news.1rj.ru/str/+m835nLa3A0c2MjBk
Telegram
leetcoders
You’ve been invited to join this group on Telegram.
"""
Given an array of integer arrays arrays where each arrays[i] is sorted in strictly increasing order,
return an integer array representing the longest common subsequence among all the arrays.
A subsequence is a sequence that can be derived from another sequence by deleting some elements
(possibly none) without changing the order of the remaining elements.
Example 1:
Input: arrays = [[1,3,4],
[1,4,7,9]]
Output: [1,4]
Explanation: The longest common subsequence in the two arrays is [1,4].
Example 2:
Input: arrays = [[2,3,6,8],
[1,2,3,5,6,7,10],
[2,3,4,6,9]]
Output: [2,3,6]
Explanation: The longest common subsequence in all three arrays is [2,3,6].
Example 3:
Input: arrays = [[1,2,3,4,5],
[6,7,8]]
Output: []
Explanation: There is no common subsequence between the two arrays.
"""
2760. Longest Even Odd Subarray With Threshold
You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions:
nums[l] % 2 == 0
For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2
For all indices i in the range [l, r], nums[i] <= threshold
Return an integer denoting the length of the longest such subarray.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3,2,5,4], threshold = 5
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Example 2:
Input: nums = [1,2], threshold = 2
Output: 1
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
Example 3:
Input: nums = [2,3,4,5], threshold = 4
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
It satisfies all the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= threshold <= 100
you can test on the leetcode
You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions:
nums[l] % 2 == 0
For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2
For all indices i in the range [l, r], nums[i] <= threshold
Return an integer denoting the length of the longest such subarray.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3,2,5,4], threshold = 5
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Example 2:
Input: nums = [1,2], threshold = 2
Output: 1
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
Example 3:
Input: nums = [2,3,4,5], threshold = 4
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
It satisfies all the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= threshold <= 100
you can test on the leetcode
LeetCode
Longest Even Odd Subarray With Threshold - LeetCode
Can you solve this real interview question? Longest Even Odd Subarray With Threshold - You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <=…
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <=…
👍2❤1
Leetcode with dani
are u ready to do another interview question?
LeetCode
Adding Spaces to a String - LeetCode
Can you solve this real interview question? Adding Spaces to a String - You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before…
Iterating through a string and appending to an empty string in Python has O(n²) time complexity because strings are immutable, and each append involves copying the entire string so far.
Optimized Approach: Use a list to collect characters and join them at the end:
This reduces the time complexity to O(n).
Optimized Approach: Use a list to collect characters and join them at the end:
result = []
for char in string:
result.append(char)
final_string = ''.join(result)
This reduces the time complexity to O(n).
👍4🔥2
Forwarded from Eunuch
Today's Interview Question```
You are given a string s, which contains stars *.
In one operation, you can:
Choose a star in s.
Remove the closest non-star character to its left, as well as remove the star itself.
Return the string after all stars have been removed.
Note:
The input will be generated such that the operation is always possible.
It can be shown that the resulting string will always be unique.
Example 1:
Input: s = "leet**cod*e"
Output: "lecoe"
Explanation: Performing the removals from left to right:
- The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e".
- The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e".
- The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe".
There are no more stars, so we return "lecoe".
Example 2:
Input: s = "erase*****"
Output: ""
Explanation: The entire string is removed, so we return an empty string.
Constraints:
1 <= s.length <= 10**5
s consists of lowercase English letters and stars *.
The operation above can be performed on s.
def fun(s):
stack = []
for c in s:
if c != "*":
stack.append(c)
else:
stack.pop()
return "".join(stack)
print(fun("erase*****"))
`👍5
Todays interview question
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.
❤4