Problem:
A string can be shortened by replacing any number of non-adjacent, non-empty substrings with their lengths (without leading zeros).
Examples of valid abbreviations:
"i12n" → (i mplementatio n)
"imp4n5n" → (imp leme n tatio n)
"14" → (implementation)
"implemetation" → (no substrings replaced)
Invalid abbreviations:
"i57n" → adjacent substrings replaced
"i012n" → leading zeros
"i0mplementation" → empty substring replaced
Your Task:
Given a word and an abbreviation abbr, return true if abbr correctly abbreviates word, otherwise return false.
A substring = a contiguous non-empty sequence of characters.
Examples
1.
Input: word = "apple", abbr = "a3e"
Output: true
2.
Input: word = "international", abbr = "i9l"
Output: false
3.
Input: word = "abbreviation", abbr = "abbreviation"
Output: true
Constraints
1 <= word.length <= 100
word contains only lowercase letters
1 <= abbr.length <= 100
abbr contains lowercase letters and digits
All digit substrings fit in a 32-bit integer #Leetcode_408_Premium
A string can be shortened by replacing any number of non-adjacent, non-empty substrings with their lengths (without leading zeros).
Examples of valid abbreviations:
"i12n" → (i mplementatio n)
"imp4n5n" → (imp leme n tatio n)
"14" → (implementation)
"implemetation" → (no substrings replaced)
Invalid abbreviations:
"i57n" → adjacent substrings replaced
"i012n" → leading zeros
"i0mplementation" → empty substring replaced
Your Task:
Given a word and an abbreviation abbr, return true if abbr correctly abbreviates word, otherwise return false.
A substring = a contiguous non-empty sequence of characters.
Examples
1.
Input: word = "apple", abbr = "a3e"
Output: true
2.
Input: word = "international", abbr = "i9l"
Output: false
3.
Input: word = "abbreviation", abbr = "abbreviation"
Output: true
Constraints
1 <= word.length <= 100
word contains only lowercase letters
1 <= abbr.length <= 100
abbr contains lowercase letters and digits
All digit substrings fit in a 32-bit integer #Leetcode_408_Premium
😨1
I am sending A2SV G7 interview questions which i got from u so u can practice similar question for your interview preparations if u want send or say share something u can send to me
Leetcode with dani pinned «I am sending A2SV G7 interview questions which i got from u so u can practice similar question for your interview preparations if u want send or say share something u can send to me»
Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".
A string is palindromic if it reads the same forward and backward.
Example 1:
Input: words = ["abc","car","ada","racecar","cool"]
A string is palindromic if it reads the same forward and backward.
Example 1:
Input: words = ["abc","car","ada","racecar","cool"]
Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.Today's question
Example 1:
Input: nums = [1,0,1,1,0]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones.
The max number of consecutive ones is 4.
Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones.
The max number of consecutive ones is 4.
Constraints:
1 <= nums.length <= 10**3
nums[i] is either 0 or 1.
Forwarded from ALX Ethiopia
The ALX x CodeLeague National Challenge was a huge success! Nearly 50 teams battled it out on Codeforces, celebrating top performers and first solvers. Big thanks to everyone who competed and pushed Ethiopian tech forward! 🚀🏆
#ALXEthiopia #CodeLeagueEthiopia #CompetitiveProgramming #Codeforces #DSA #ALXAfrica #LifeAtALX
#ALXEthiopia #CodeLeagueEthiopia #CompetitiveProgramming #Codeforces #DSA #ALXAfrica #LifeAtALX
⚡2
'''
You have some apples and a basket that can carry up to 5000 units of weight.
Given an integer array weight where weight[i] is the weight of the ith apple, return the maximum number of apples you can put in the basket.
Example 1:
Input: weight = [100,200,150,1000]
Output: 4
Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.
Example 2:
Input: weight = [900,950,800,1000,700,800]
Output: 5
Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
'''
weight = [100,200,150,5000]
def max_apple(weight):
left = 0
sum = 0
weight.sort()
while left < len(weight):
sum += weight[left]
if sum > 5000:
return left
else:
left +=1
return left
w = max_apple(weight)
print(w)
You have some apples and a basket that can carry up to 5000 units of weight.
Given an integer array weight where weight[i] is the weight of the ith apple, return the maximum number of apples you can put in the basket.
Example 1:
Input: weight = [100,200,150,1000]
Output: 4
Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.
Example 2:
Input: weight = [900,950,800,1000,700,800]
Output: 5
Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
'''
weight = [100,200,150,5000]
def max_apple(weight):
left = 0
sum = 0
weight.sort()
while left < len(weight):
sum += weight[left]
if sum > 5000:
return left
else:
left +=1
return left
w = max_apple(weight)
print(w)
'''
Given an integer array nums, return true if nums is consecutive, otherwise return false. An array is consecutive if it contains every number in the range [x, x + n - 1] (inclusive), where x is the minimum number in the array and n is the length of the array.
Example 1:
Input: nums = [1,3,4,2]
1 - 0
2 - 1
index = val - min
for i in range(x,x+n-1+1):
if arr[i - x] != i
return False
return True
mn = 1
len = 4
[1,4]
Output: true
Explanation: The minimum value is 1 and the length of nums is 4. All of the values in the range [x, x + n - 1] = [1, 1 + 4 - 1] = [1, 4] = (1, 2, 3, 4) occur in nums.
Therefore, nums is consecutive.
Example 2:
Input: nums = [1,3]
mn = 1
len = 2
[1,2]
Output: false
Explanation: The minimum value is 1 and the length of nums is 2. The value 2 in the range [x, x + n - 1] = [1, 1 + 2 - 1], = [1, 2] = (1, 2) does not occur in nums.
Therefore, nums is not consecutive.
Example 3:
Input: nums = [3,5,4]
Output: true
Explanation: The minimum value is 3 and the length of nums is 3. All of the values in the range [x, x + n - 1] = [3, 3 + 3 - 1] = [3, 5] = (3, 4, 5) occur in nums. Therefore, nums is consecutive.
Constraints:
1 <= nums.length <= 10**5
0 <= nums[i] <= 10**5
approach
- prepare an arr that contains all the elemets in the range
- sort my array
'''
def check_consecutive(nums):
n = len(nums)
nums.sort()
minVal = nums[0]
for val in range(minVal, minVal + n ):
index = val - minVal
if nums[index] != val:
return False
return True
# time-compexity
nums = [1,3,4,2]
print(check_consecutive(nums))
Given an integer array nums, return true if nums is consecutive, otherwise return false. An array is consecutive if it contains every number in the range [x, x + n - 1] (inclusive), where x is the minimum number in the array and n is the length of the array.
Example 1:
Input: nums = [1,3,4,2]
1 - 0
2 - 1
index = val - min
for i in range(x,x+n-1+1):
if arr[i - x] != i
return False
return True
mn = 1
len = 4
[1,4]
Output: true
Explanation: The minimum value is 1 and the length of nums is 4. All of the values in the range [x, x + n - 1] = [1, 1 + 4 - 1] = [1, 4] = (1, 2, 3, 4) occur in nums.
Therefore, nums is consecutive.
Example 2:
Input: nums = [1,3]
mn = 1
len = 2
[1,2]
Output: false
Explanation: The minimum value is 1 and the length of nums is 2. The value 2 in the range [x, x + n - 1] = [1, 1 + 2 - 1], = [1, 2] = (1, 2) does not occur in nums.
Therefore, nums is not consecutive.
Example 3:
Input: nums = [3,5,4]
Output: true
Explanation: The minimum value is 3 and the length of nums is 3. All of the values in the range [x, x + n - 1] = [3, 3 + 3 - 1] = [3, 5] = (3, 4, 5) occur in nums. Therefore, nums is consecutive.
Constraints:
1 <= nums.length <= 10**5
0 <= nums[i] <= 10**5
approach
- prepare an arr that contains all the elemets in the range
- sort my array
'''
def check_consecutive(nums):
n = len(nums)
nums.sort()
minVal = nums[0]
for val in range(minVal, minVal + n ):
index = val - minVal
if nums[index] != val:
return False
return True
# time-compexity
nums = [1,3,4,2]
print(check_consecutive(nums))
'''
There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.
Note that multiple kids can have the greatest number of candies.
Example 1:
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
Example 2:
Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
Example 3:
Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
Constraints:
n == candies.length
2 <= n <= 100
1 <= candies[i] <= 100
1 <= extraCandies <= 50
'''
##function to add the extraccandies to each elements and compare
def compare_candies(arr, extra_candies):
max_candy = max(arr)
result = []
for num in arr:
result.append(num + extra_candies >= max_candy)
return result
candies = [2,3,5,1,3]
extraCandies = 3
print(compare_candies(candies, extraCandies))
❤1