Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: queue = [] i = ans = 0 for j in range(len(A)): if(A[j] != 1): if(len(queue) == K): try: i = queue.pop(0)+1 except: i=j+1 if(len(queue) < K): queue.append(j) ans = max(ans, j-i+1) return ans
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
from collections import deque class Solution: def longestOnes(self, A: List[int], K: int) -> int: q=deque() cnt=0 max_cnt=0 for i in range(len(A)): if A[i]==0: q.append(i) if len(q)>K: max_cnt=max(max_cnt,cnt) cnt=i-q[0] q.popleft() else: cnt+=1 else: cnt+=1 max_cnt=max(max_cnt,cnt) return max_cnt
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: l = 0 r = 0 m = len(A) count = 0 max_count = 0 while r < m: if A[r] == 1: count += 1 while r - l + 1 - count > K: if A[l] == 1: count -= 1 l += 1 max_count = max(r-l+1, max_count) r += 1 return max_count
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A, K): if not A: return 0 n = len(A) right = 0 cnt = 0 res = 0 for left in range(n): while right <= n - 1 and (cnt < K or A[right] == 1): cnt += (A[right] == 0) right += 1 res = max(res, right - left) cnt -= (A[left] == 0) return res
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: pre_sum = [A[0]] for i in range(1,len(A)): pre_sum.append(pre_sum[-1]+A[i]) def cal(l,r): if (l == 0): return pre_sum[r] else: return pre_sum[r] - pre_sum[l-1] l = 0 r = 0 ans = K while (l<len(A)): #print (l,r) while (r<len(A) and (r-l+1)-cal(l,r)<=K ): ans = max(ans,r-l+1) r = r + 1 #print (r) if (r==len(A)): break while ( l<len(A) and (r-l+1)-cal(l,r)>K ): l = l + 1 #print (l) return ans
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: i=j=maxCount=maxlen=0 d={0:0,1:0} while(i<len(A)): d[A[i]]+=1 while(d[0]>K): d[A[j]]-=1 j+=1 maxlen=max(maxlen,i-j+1) i+=1 return maxlen
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: i, ans = 0, 0 for j, num in enumerate(A): if num == 0: K -= 1 while K < 0: if A[i] == 0: K += 1 i += 1 ans = max(ans, j-i+1) return ans ans = 0 start, end = 0, 0 while end < len(A): if A[end] == 1: ans = max(ans, end-start+1) end+=1 else: if K != 0: ans = max(ans, end-start+1) end+=1 K -= 1 else: if A[start] == 0: K+=1 start+=1 return ans
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: le, ri = 0, 0 max_len = 0 while ri < len(A): if A[ri] == 0: K -= 1 ri += 1 if K >= 0: max_len= max(max_len, ri - le) print((le, ri)) while K < 0 and le < ri: if A[le] == 0: K += 1 le += 1 return max_len
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: left,right = 0,0 num_zeros = 0 window_size = 0 while right < len(A): if A[right] == 0: num_zeros += 1 while num_zeros > K: if A[left] == 0: num_zeros -= 1 left += 1 window_size = max(window_size,right-left+1) right += 1 return window_size
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
import math import random import string from functools import lru_cache from operator import truediv from typing import List import collections import functools import heapq import itertools import sys from functools import lru_cache from typing import List import numpy as np import collections import functools import heapq import itertools import sys from functools import lru_cache from typing import List ''' 反转K个位置的0为1,问最长连续1的长度。 Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. 思路:滑动窗口。 主要思想,开区间伸缩左右边界。闭区间不好处理初始情况。 ''' class Solution: def longestOnes(self, A: List[int], K: int) -> int: # 开区间[start, end) start = 0 end = 0 ret = 0 while end < len(A): if A[end] == 1: end += 1 else: if K > 0: end += 1 K -= 1 else: if A[start] == 0: K += 1 start += 1 ret = max(ret, end - start) return ret # A = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1] # K = 3 # # r = Solution() # a = r.longestOnes(A, K) # print(a)
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) vowel = set(['a','e','i','o','u']) i=0 res = 0 while i<k: if s[i] in vowel: res+=1 i+=1 j=k i=0 maxV = res while j<n: if s[i] in vowel: res-=1 if s[j] in vowel: res+=1 i+=1 j+=1 if maxV<res: maxV = res return maxV
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = set(['A','E','I','O','U','a','e','i','o','u']) res = 0 cnt = 0 for i in range(k): if s[i] in vowels: cnt += 1 res = max(res, cnt) for i in range(k, len(s)): if s[i] in vowels: cnt += 1 if s[i-k] in vowels: cnt -= 1 res = max(res, cnt) return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = set(['A','E','I','O','U','a','e','i','o','u']) res = 0 cnt = 0 for i in range(k): if s[i] in vowels: cnt += 1 res = max(res, cnt) for i in range(k, len(s)): if s[i] in vowels: cnt += 1 if s[i-k] in vowels: cnt -= 1 res = max(res, cnt) return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: maxNum = -1 currNum = 0 vowels = 'aeiou' j=0 while j <k: if s[j] in vowels: currNum+=1 j+=1 maxNum = max(maxNum, currNum) for i in range(k,len(s)): if s[i] in vowels: currNum+=1 if s[i-k] in vowels: currNum-=1 maxNum = max(maxNum, currNum) return maxNum
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = {'a','e','i','o','u'} cnt = 0 for i in range(k): if s[i] in vowel: cnt += 1 if cnt == k: return k i = 0 res = cnt for j in range(k,len(s)): if s[i] in vowel: cnt -= 1 if s[j] in vowel: cnt += 1 i += 1 if cnt == k: return k res = max(res, cnt) return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = 'aeiou' n = len(s) i = k window = s[:k] res = sum([1 for x in window if x in vowel]) tmp = res while i <n: if s[i-k] in vowel: tmp -= 1 if s[i] in vowel: tmp += 1 res = max(res, tmp) i += 1 return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = set('aeiou') init = len([x for x in s[:k] if x in vowels]) ans = init for i in range(1, len(s)-k+1): if s[i-1] in vowels: init -= 1 if s[i+k-1] in vowels: init += 1 ans = max(ans, init) return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: ans=0 dic={'a':1,'e':1,'i':1,'o':1,'u':1} for i in range(k): if s[i] in dic: ans+=1 i=0 j=k temp=ans while j<len(s): if s[j] in dic: temp+=1 if s[i] in dic: temp-=1 ans=max(ans,temp) i+=1 j+=1 return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: # Sliding window with fixed width (=k) vowels = 'aeiou' num_vowels = sum(1 for i in range(k) if s[i] in vowels) max_vowels = num_vowels for removed_idx in range(0, len(s) - k): added_idx = removed_idx + k num_vowels += int(s[added_idx] in vowels) - int(s[removed_idx] in vowels) if num_vowels > max_vowels: max_vowels = num_vowels return max_vowels
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {'a','e','i','o','u'} vowel_count = 0 for i in range(k): if s[i] in vowels: vowel_count += 1 max_vowels = vowel_count for i in range(k, len(s)): if s[i-k] in vowels: vowel_count -= 1 if s[i] in vowels: vowel_count += 1 max_vowels = vowel_count if vowel_count > max_vowels else max_vowels return max_vowels
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: truth_table = [] for idx, char in enumerate(s): if char in ['a','e','i','o','u']: truth_table.append(idx) pre_count, pre_idx, return_count = 0, 0, 0 for idx, vowel_idx in enumerate(truth_table): if idx == 0: count = 1 next_idx = idx + 1 else: count = pre_count - 1 next_idx = pre_idx + 1 while (next_idx < len(truth_table) and truth_table[next_idx] - vowel_idx + 1 <= k): count += 1 pre_idx = next_idx next_idx += 1 pre_count = count if count > return_count: return_count = count return return_count
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: # Bounded sliding window solution vowel = set('aeiou') leftwindow = 0 ans = 0 temp = 0 for rightwindow in range(len(s)): # expand the window if rightwindow < k: if s[rightwindow] in vowel: temp += 1 ans = max(temp, ans) continue leftChar = s[leftwindow] rightChar = s[rightwindow] if rightChar in vowel and leftChar in vowel: pass elif rightChar in vowel: temp += 1 elif leftChar in vowel: temp -= 1 ans = max(temp, ans) leftwindow += 1 return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: maxVow, count = 0, 0 for i, j in enumerate(s): if j in 'aeiou': count += 1 if i >= k and s[i-k] in 'aeiou': count -= 1 maxVow = max(maxVow, count) return maxVow
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels=['a','e','i','o','u'] max_cnt=sum([1 for i in range(k) if s[i] in vowels]) cnt=max_cnt for i in range(k,len(s)): if s[i] in vowels: cnt+=1 if s[i-k] in vowels: cnt-=1 max_cnt=max(cnt,max_cnt) return max_cnt
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: ans=0 cur=0 for j,c in enumerate(s): cur+=c in 'aeiou' if j>=k: cur-=s[j-k] in 'aeiou' ans=max(ans,cur) return max(ans,cur)
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
''' asdadrwsg3234 l r ''' class Solution: def maxVowels(self, s: str, k: int) -> int: vo = set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']) l, max_l, count = 0, 0, 0 for i in range(len(s)): ch = s[i] if ch in vo: count += 1 if i - l + 1 > k: if s[l] in vo: count -= 1 l += 1 max_l = max(max_l, count) return max_l
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = 'aeiou' i,tmp,res = 0,0,0 for j in range(len(s)): if s[j] in vowels: tmp += 1 while j-i+1 > k: if s[i] in vowels: tmp -= 1 i += 1 res = max(res,tmp) return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: su,res,vov = 0,0,('a', 'e', 'i', 'o','u') for i,v in enumerate(s): if v in vov: su += 1 if i-k >= 0: if s[i-k] in vov: su -= 1 res = max(res,su) return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowelMap = { 'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0 } for i in range(0, k): if s[i] in vowelMap: vowelMap[s[i]] += 1 maxVowels = sum(vowelMap.values()) for i in range(1, len(s)-k+1): if s[i+k-1] in vowelMap: vowelMap[s[i+k-1]] += 1 if s[i-1] in vowelMap: vowelMap[s[i-1]] -= 1 maxVowels = max(maxVowels, sum(vowelMap.values())) return maxVowels
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: m, beg, end = len(s), 0, 0 vowels = ['a', 'e', 'i', 'o', 'u'] maxi, count = 0, 0 while end < m: if s[end] in vowels: count += 1 if end-beg+1 >= k: maxi = max(maxi, count) if s[beg] in vowels: count -= 1 beg += 1 end += 1 return maxi
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {'a','e','i','o','u'} mx, vowelCount = 0, 0 l,r = 0,0 while(r < len(s)): if s[r] in vowels: vowelCount +=1 r += 1 while(r - l == k and l < len(s)): mx = max(vowelCount,mx) if s[l] in vowels: vowelCount -= 1 l+=1 return mx
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: def vowel_letters(ch): if ch in ['a','e','i','o','u']: return True return False temp = [1 if vowel_letters(i) else 0 for i in s] su = sum(temp[:k]) m = su for i in range(k,len(temp)): su += temp[i]-temp[i-k] m = max([su,m]) return m
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: ans = 0 n = len(s) i, j = 0, 0 cnt = 0 vowels = {'a', 'e', 'i', 'o', 'u'} while j < n: if s[j] in vowels: cnt += 1 if j == k-1: break else: j += 1 ans = max(ans, cnt) while j+1 < n: if s[i] in vowels: cnt -= 1 i += 1 j += 1 if s[j] in vowels: cnt += 1 ans = max(ans, cnt) return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: total=res=0 for i in range(len(s)): if s[i] in 'aeiou': total+=1 if i>=k-1: res=max(res,total) if s[i-k+1] in 'aeiou': total-=1 return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self,S,K): N=len(S) L=[0]*(N+1) for i in range(N): if S[i] in ['a','e','i','o','u']: L[i+1]=1 for i in range(1,N+1): L[i]+=L[i-1] i=0 j=K ans=0 while(i<=N and j<=N): ans=max(ans,L[j]-L[i]) i+=1 j+=1 return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: # Sliding window. ret = 0 cnt = 0 ## initial window for i in range(k): if self.vowel(s[i]): cnt += 1 ret = max(ret, cnt) ## move the window for i in range(k, len(s)): if self.vowel(s[i]): cnt += 1 if self.vowel(s[i-k]): cnt -= 1 ret = max(ret, cnt) return ret def vowel(self, char): return char in ['a', 'e', 'i', 'o', 'u']
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: # k = substring length. If sliding windows then left & right of sliding window should be different by k def maxVowels(self, s: str, k: int) -> int: is_a_vowel = lambda letter: letter in 'aeiou' count = 0 maximum_vowels = 0 left = 0 for right in range(len(s)): # moving right but not touching left pointer if right < k: count = count + 1 if is_a_vowel(s[right]) else count # add next vowel if present maximum_vowels = max(maximum_vowels, count) else: # moving right and touching left pointer so that distance between pointers is always k count = max(0, count-1) if is_a_vowel(s[left]) else count # letter is cut from substrg of k, so decrease count but not lower than 0 count = count + 1 if is_a_vowel(s[right]) else count # add next vowel if present left = left + 1 maximum_vowels = max(maximum_vowels, count) return maximum_vowels
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: # Sliding window. ret = 0 cnt = 0 ## initial window for i in range(k): if self.vowel(s[i]): cnt += 1 ret = max(ret, cnt) ## move the window for i in range(k, len(s)): if self.vowel(s[i]): cnt += 1 if self.vowel(s[i-k]): cnt -= 1 ret = max(ret, cnt) return ret def vowel(self, char): return char in ['a', 'e', 'i', 'o', 'u']
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s, k): vcnt, res = dict(), 0 for i, c in enumerate(s): if i-k >= 0 and s[i-k] in 'aeiou': vcnt[s[i-k]] -= 1 if c in 'aeiou': vcnt[c] = vcnt.get(c, 0) + 1 res = max(res, sum(vcnt.values())) return res class Solution: def maxVowels(self, s, k): res, cnt = 0, 0 for i, c in enumerate(s): # if i-k >= 0 and s[i-k] in 'aeiou': # cnt -= 1 # if c in 'aeiou': # cnt += 1 cnt += (c in 'aeiou') - (i-k >= 0 and s[i-k] in 'aeiou') #res = max(res, cnt) if cnt > res: res = cnt return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
# class Solution: # def maxVowels(self, s: str, k: int) -> int: # vowels = {\"a\", \"e\", \"i\", \"o\", \"u\"} # m = 0 # for el in s[:k]: # if el in vowels: # m += 1 # a = m # for i in range(k, len(s)): # if s[i] in vowels: # a += 1 # if s[i-k] in vowels: # a -= 1 # m = max(a,m) # return m class Solution: # k = substring length. If sliding windows then left & right of sliding window should be different by k def maxVowels(self, s: str, k: int) -> int: is_a_vowel = lambda letter: letter in 'aeiou' count = 0 maximum_vowels = 0 left = 0 for right in range(len(s)): # moving right but not touching left pointer if right < k: count = count + 1 if is_a_vowel(s[right]) else count # add next vowel if present maximum_vowels = max(maximum_vowels, count) else: # moving right and touching left pointer so that distance between pointers is always k count = max(0, count-1) if is_a_vowel(s[left]) else count # letter is cut from substrg of k, so decrease count but not lower than 0 count = count + 1 if is_a_vowel(s[right]) else count # add next vowel if present left = left + 1 maximum_vowels = max(maximum_vowels, count) return maximum_vowels
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: max_val = 0 tmp = 0 i = 0 he = len(s) for i in range(k): if s[i] in 'aeiou': tmp += 1 if tmp == k: return k if tmp > max_val: max_val = tmp for i in range(k,he): if s[i-k] in 'aeiou': tmp -= 1 if s[i] in 'aeiou': tmp += 1 if tmp > max_val: max_val = tmp return max_val
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: i = 0 j = i+k-1 cc = 0 for k in range(i,j+1): if s[k] == 'a' or s[k] == 'e' or s[k] == 'i' or s[k] == 'o' or s[k] == 'u': cc += 1 max_c = 0 while j<len(s): if cc > max_c: max_c = cc if s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u': cc -= 1 i += 1 j += 1 if j<len(s): if s[j] == 'a' or s[j] == 'e' or s[j] == 'i' or s[j] == 'o' or s[j] == 'u': cc += 1 return max_c
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s, k): vcnt, res = dict(), 0 for i, c in enumerate(s): if i-k >= 0 and s[i-k] in 'aeiou': vcnt[s[i-k]] -= 1 if c in 'aeiou': vcnt[c] = vcnt.get(c, 0) + 1 #res = max(res, sum(vcnt.values())) if sum(vcnt.values()) > res: res = sum(vcnt.values()) return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {'a', 'e', 'i', 'o', 'u'} current = 0 maxx = 0 start, end = 0, 0 if k == 0: return 0 while end < min(start + k, len(s)): if s[end] in vowels: current += 1 end += 1 if end == len(s): return current while end < len(s): maxx = max(maxx, current) if s[start] in vowels: current -= 1 start += 1 if s[end] in vowels: current += 1 end += 1 return max(maxx, current)
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s, k): vcnt, res = dict(), 0 for i, c in enumerate(s): if i-k >= 0 and s[i-k] in 'aeiou': vcnt[s[i-k]] -= 1 if c in 'aeiou': vcnt[c] = vcnt.get(c, 0) + 1 res = max(res, sum(vcnt.values())) return res # class Solution: # def maxVowels(self, s, k): # res, cnt = 0, 0 # for i, c in enumerate(s): # if i-k >= 0 and s[i-k] in 'aeiou': # cnt -= 1 # if c in 'aeiou': # cnt += 1 # #res = max(res, cnt) # if cnt > res: res = cnt # return res # class Solution: # def maxVowels(self, s, k): # cnt, res, N = 0, 0, len(s) # for i, c in enumerate(s[:k]): # if c in 'aeiou': # cnt += 1 # res = cnt # for c1, c2 in zip(s[:-k], s[k:]): # cnt += (c2 in 'aeiou') - (c1 in 'aeiou') # #res = max(res, cnt) # if cnt > res: res = cnt # return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = 0 vowelString = ['a','e','i','o','u'] end = 0 start = 0 maxx = 0 while end < len(s): if s[end] in vowelString: vowels += 1 if end >= k: if s[start] in vowelString: vowels -= 1 start += 1 end += 1 maxx = max(maxx,vowels) return maxx
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel=0 max_vowel=0 front=0 for i in range(len(s)): if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u': vowel+=1 print((s[i])) if i>=k-1: max_vowel=max(max_vowel,vowel) front=s[i-k+1] #print(vowel,i) if front=='a' or front== 'e' or front=='i' or front=='o' or front=='u': vowel-=1 return max_vowel
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: if len(s) < k: k = len(s) cnt = 0 for i in range(k): if s[i] in ('a', 'e', 'i', 'o', 'u'): cnt += 1 max_cnt = cnt for i in range(k, len(s)): if s[i] in ('a', 'e', 'i', 'o', 'u'): cnt += 1 if s[i-k] in ('a', 'e', 'i', 'o', 'u'): cnt -= 1 max_cnt = max(max_cnt, cnt) return max_cnt
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: if not s or not k: return 0 max_vowels = 0 start_pointer = 0 end_pointer = k - 1 vowels = {'a', 'e', 'i', 'o', 'u'} num_curr_window_vowels = 0 for i in range(k): if s[i] in vowels: num_curr_window_vowels += 1 max_vowels = max(max_vowels, num_curr_window_vowels) while end_pointer < len(s) - 1: if s[start_pointer] in vowels: num_curr_window_vowels -= 1 start_pointer += 1 end_pointer += 1 if s[end_pointer] in vowels: num_curr_window_vowels += 1 max_vowels = max(max_vowels, num_curr_window_vowels) return max_vowels
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
def window_sums(bits, width): coll = [] pointer = 0 total = 0 for elem in bits: if len(coll) < width: coll.append(elem) total += elem if len(coll) == width: yield total else: total += elem - coll[pointer] coll[pointer] = elem pointer = (pointer + 1) % width yield total class Solution: def maxVowels(self, s: str, k: int) -> int: bits = (ch in 'aeiou' for ch in s) sums = window_sums(bits, k) return max(sums)
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s, k): vcnt, res = dict(), 0 for i, c in enumerate(s): if i-k >= 0 and s[i-k] in 'aeiou': vcnt[s[i-k]] -= 1 if c in 'aeiou': vcnt[c] = vcnt.get(c, 0) + 1 #res = max(res, sum(vcnt.values())) if sum(vcnt.values()) > res: res = sum(vcnt.values()) return res # class Solution: # def maxVowels(self, s, k): # res, cnt = 0, 0 # for i, c in enumerate(s): # if i-k >= 0 and s[i-k] in 'aeiou': # cnt -= 1 # if c in 'aeiou': # cnt += 1 # #res = max(res, cnt) # if cnt > res: res = cnt # return res # class Solution: # def maxVowels(self, s, k): # cnt, res, N = 0, 0, len(s) # for i, c in enumerate(s[:k]): # if c in 'aeiou': # cnt += 1 # res = cnt # for c1, c2 in zip(s[:-k], s[k:]): # cnt += (c2 in 'aeiou') - (c1 in 'aeiou') # #res = max(res, cnt) # if cnt > res: res = cnt # return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = set({'a','e','i','o','u'}) curr = 0 maxx = 0 for i in range(k): if s[i] in vowel: curr+=1 maxx+=1 for i in range(1, len(s)-k+1): if s[i+k-1] in vowel: curr +=1 if s[i-1] in vowel: curr -=1 if curr == k: return curr if curr >= maxx: maxx = curr return maxx
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {'a', 'e', 'i', 'o', 'u'} count = 0 maxCount = 0 i = 0 for j in range(len(s)): if s[j] in vowels: count += 1 if j >= k and s[j-k] in vowels: count -= 1 maxCount = max(maxCount, count) return maxCount
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: maks=0 string='' count=0 lenght=0 for l in s: if l=='a' or l=='e' or l=='i' or l=='o' or l=='u': count+=1 string+=l lenght+=1 if lenght==k: if count>maks: maks=count if string[0]=='a' or string[0]=='e' or string[0]=='i' or string[0]=='o' or string[0]=='u': count-=1 lenght-=1 string=string[1:k] return maks
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: self.vowels = ['a', 'e', 'i', 'o', 'u'] def isVowel(char): return char in self.vowels i = 0 j = 0 cur_count = 0 result = 0 length = len(s) while i < length and j < length: if isVowel(s[j]): cur_count += 1 if j - i + 1 >= k: result = max(result, cur_count) if isVowel(s[i]): cur_count -= 1 i += 1 j += 1 return result
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: a=s[:k] d={'a':0,'e':0,'i':0,'o':0,'u':0} def checkVowels(): total=0 for key in d: total+=d[key] return total for c in s[:k]: if c in d: d[c]+=1 total=checkVowels() i=1 while(i<=len(s)-k): prev=s[i-1] if prev in d: d[prev]-=1 nxt=s[i+k-1] if nxt in d: d[nxt]+=1 #print('i',i,'d',d) total=max(total,checkVowels()) i+=1 return total
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: currMax = 0 curr = 0 vowels = set(['a','e','i','o','u']) for i,c in enumerate(s): if c in vowels: curr += 1 if i >= k and s[i-k] in vowels: curr -= 1 currMax = max(curr, currMax) if currMax == k: break return currMax
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: memo = collections.defaultdict(lambda : 0) i = 0 res = 0 vowels = set('aeiou') for j in range(len(s)): if s[j] in vowels: memo[s[j]] += 1 if j-i+1==k: res = max(res, sum(memo.values())) if s[i] in memo: memo[s[i]] -= 1 if memo[s[i]] ==0: del memo[s[i]] i += 1 return res
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: cnt = i = ans = 0 for j in range(len(s)): c = s[j] cnt += (s[j] in 'aeiou') if j >= k: cnt -= (s[i] in 'aeiou') i += 1 if j >= k - 1: ans = max(ans, cnt) return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: temp = defaultdict(lambda : 0) for i in range(k): temp[s[i]] += 1 ans = sum(temp[i] for i in 'aeiou') for i in range(k, len(s)): temp[s[i-k]] -= 1 temp[s[i]] += 1 ans = max(ans, sum(temp[i] for i in 'aeiou')) return ans
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
from collections import Counter class Solution: def maxVowels(self, s: str, k: int) -> int: c = Counter(s[:k]) m = sum([c[x] for x in 'aeiou']) for i in range(len(s) - k): c[s[i]] -= 1 c[s[i + k]] += 1 m = max(m, sum([c[x] for x in 'aeiou'])) return m
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: _start = 0 _end = 0 _max = 0 start = 0 length = 0 count = 0 for end in range(len(s)): length += 1 if self.check(s[end]): count += 1 if length > k: if self.check(s[start]): count -= 1 start += 1 if count > _max: _max = count _start = start _end = end return _max def check(self, ch): vowels = ['a', 'e', 'i', 'o', 'u'] for vowel in vowels: if ch == vowel: return True return False
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: l = 0 r = 0 v_set = {'a','e','i','o','u'} window_len = 0 count = 0 max_len = 0 while r < len(s): if window_len < k: if s[r] in v_set: count += 1 window_len+=1 else: print((window_len,count,s[r])) if count == k: max_len = k return max_len else: max_len = max(count,max_len) if s[l] in v_set: count-=1 l+=1 window_len-=1 if s[r] in v_set: count += 1 window_len+=1 r+=1 max_len = max(count,max_len) return max_len
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: _max = 0 start = 0 length = 0 count = 0 for end in range(len(s)): length += 1 if self.check(s[end]): count += 1 if length > k: if self.check(s[start]): count -= 1 start += 1 _max = max(_max, count) return _max def check(self, ch): vowels = ['a', 'e', 'i', 'o', 'u'] for vowel in vowels: if ch == vowel: return True return False
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: i=0 j=0 cur_max=0 maxx=0 for i in range(len(s)): if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u': cur_max+=1 if i-j+1>k: if s[j]=='a' or s[j]=='e' or s[j]=='i' or s[j]=='o' or s[j]=='u': cur_max-=1 maxx=max(cur_max,maxx) j+=1 maxx=max(cur_max,maxx) return maxx
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: cache=Counter(A) c_list=sorted(list(cache),key=abs) for x in c_list: if cache[x]>cache[2*x]: return False cache[2*x]-=cache[x] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: cache=collections.Counter(A) c_list=sorted(list(cache),key=abs) for x in c_list: if cache[x]>cache[2*x]: return False cache[2*x]-=cache[x] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
from collections import Counter class Solution: def canReorderDoubled(self, A: List[int]) -> bool: if len(A) % 2 == 1: return False if not A: return True nums = sorted(set(A)) nums = [x for x in nums if x < 0][::-1] + [x for x in nums if x >= 0] c = Counter(A) done = set() for num in nums: if num in done: continue if 2*num not in c: return False if c[2*num] < c[num]: return False c[2*num] -= c[num] if c[2*num] == 0: done.add(2*num) c[num] = 0 done.add(num) return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
import collections class Solution: def canReorderDoubled(self, A: List[int]) -> bool: ''' Place all values into dicts and start counting from the bottom ''' if not A: return True pos = collections.defaultdict(int) neg = collections.defaultdict(int) zero = 0 # O(n) for v in A: if v > 0: pos[v] += 1 if v < 0: neg[-v] += 1 if v == 0: zero += 1 b = self.check(pos) if not b: return False b = self.check(neg) if not b: return False return zero % 2 == 0 def check(self, d): # worst case O(n), done n times while d: v = min(d) if not 2 * v in d: return False if d[v] > d[2 * v]: return False elif d[v] == d[2*v]: del d[v] del d[2*v] else: d[2*v] -= d[v] del d[v] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: ''' Place all values into dicts and start counting from the bottom ''' if not A: return True pos = {} neg = {} zero = 0 # O(n) for v in A: if v > 0: pos[v] = pos.get(v, 0) + 1 if v < 0: neg[-v] = neg.get(-v, 0) + 1 if v == 0: zero += 1 b = self.check(pos) if not b: return False b = self.check(neg) if not b: return False return zero % 2 == 0 def check(self, d): # worst case O(n), done n times while d: v = min(d) if not 2 * v in d: return False if d[v] > d[2 * v]: return False elif d[v] == d[2*v]: del d[v] del d[2*v] else: d[2*v] -= d[v] del d[v] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: i = 0 j = 0 A.sort() while i < len(A) and A[i] <= 0: if A[i] % 2 != 0: return False while j < len(A) and A[j] < 0 and 2 * A[j] < A[i]: j += 1 if j >= len(A) or 2 * A[j] > A[i]: return False A[j] = None j += 1 i += 1 while i < len(A) and A[i] is None: i += 1 while i < len(A): while j < len(A) and A[j] < 2 * A[i]: j += 1 if j >= len(A) or A[j] > 2 * A[i]: return False A[j] = None j += 1 i += 1 while i < len(A) and A[i] is None: i += 1 return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: count = {} zero_count = 0 for num in A: if num == 0: zero_count += 1 elif num in count: count[num] += 1 else: count[num] = 1 if zero_count % 2 != 0: return False A.sort() for num in A: double = num * 2 if num in count and double in count: count[double] -= 1 count[num] -= 1 if count[double] == 0: count.pop(double) if count[num] == 0: count.pop(num) return count == {}
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: if len(A) % 2 == 1: return False count = collections.defaultdict(int) for num in A: count[num] += 1 A.sort() for num in A: if count[num] > 0: # print(f\"checking {num}\") double = num * 2 # print(half, double) if double in count and count[double] > 0: # print(\"found\", double) count[num] -= 1 count[double] -= 1 # print(count) # print(\"all good\") # print(count) for num in count: if count[num] != 0: return False return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: count = collections.Counter(A) for x in sorted(A, key=abs): if(count[x] == 0): continue if(count[2*x] == 0): return False count[x] -= 1 count[2*x] -= 1 return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: cache=dict() for a in A: cache[a]=cache.get(a,0)+1 while len(cache)>0: temp=[abs(k) for k in cache.keys()] curr=min(temp) if curr==0: if cache[0]%2>0: return False else: cache.pop(0) else: if curr in cache: if cache[curr]==0: cache.pop(curr) elif (2*curr not in cache) or cache[2*curr]<cache[curr]: return False else: cache[2*curr]-=cache[curr] cache.pop(curr) if -curr in cache: if cache[-curr]==0: cache.pop(-curr) elif (-2*curr not in cache) or cache[-2*curr]<cache[-curr]: return False else: cache[-2*curr]-=cache[-curr] cache.pop(-curr) return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: ''' Place all values into dicts and start counting from the bottom ''' counter = collections.Counter(A) for v in sorted(counter, key = abs): if counter[2 * v] < counter[v]: return False counter[2 * v] -= counter[v] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: counts = defaultdict(int) for x in sorted(A): counts[x] += 1 if x < 0 and x * 2 in counts: while counts[x] > 0 and counts[x * 2] > 0: counts[x] -= 1 counts[x * 2] -= 1 if x >= 0 and x / 2 in counts: while counts[x] > 0 and counts[x / 2] > 0: counts[x] -= 1 counts[x / 2] -= 1 return sum(c for v, c in counts.items()) == 0
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
import collections class Solution: def canReorderDoubled(self, A: List[int]) -> bool: ''' Place all values into dicts and start counting from the bottom ''' if not A: return True counter = collections.Counter(A) for v in sorted(counter, key = abs): if counter[2 * v] < counter[v]: return False counter[2 * v] -= counter[v] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: result = True numLen = len(A) C = Counter(A) val = list(C.keys()) val.sort() for i in range(len(val)): if val[i] < 0: if val[i] % 2 == 0: if val[i]/2 in val and C[val[i]/2] >= C[val[i]]: C[val[i] / 2] = C[val[i]/2] - C[val[i]] C[val[i]] = 0 else: result = False # break else: if val[i] * 2 in val and C[val[i]*2] >= C[val[i]]: C[val[i] * 2] = C[val[i]*2] - C[val[i]] C[val[i]] = 0 else: result = False # Break nums = list(C.values()) if nums == [0]*len(nums): result = True else: result = False return result
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: dd = Counter(A) A.sort(key = lambda a : abs(a)) for a in A: if a in dd and dd[a]: if 2*a in dd and dd[2*a]: dd[a] -= 1 dd[2*a] -= 1 if dd[a] == 0: del dd[a] if dd[a*2] == 0: del dd[a*2] else: return False return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: pos = [x for x in A if x >= 0] neg = [x for x in A if x < 0] for L in (pos,neg): count = collections.Counter(L) for x in sorted(L, key=abs): if count[x] == 0: continue if count[2*x] == 0: return False count[x] -= 1 count[2*x] -= 1 return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: A.sort(key=lambda x: (x>0, abs(x))) right = 1 size = len(A) for i in range(size): if A[i] is None: continue while right < size and 2 * A[i] != A[right]: right += 1 if right == size: return False A[right] = None return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, arr): arr.sort() negl = [x for x in arr if x < 0][::-1] posl = [x for x in arr if x >= 0] cnt = {} for x in arr: if x not in cnt: cnt[x] = 1 else: cnt[x] = cnt[x] + 1 while True : x = None if negl != [] : x = negl.pop(0) else: if posl != [] : x = posl.pop(0) else: break if cnt[x] == 0 : continue y = x * 2 if y not in cnt: return False else: if cnt[y] > 0 and cnt[x] > 0: cnt[y] = cnt[y] - 1 cnt[x] = cnt[x] - 1 else: return False return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: seen = {} A.sort() for i in A: #Logic for checking half and double of number if i/2 in seen: seen[i/2] -= 1 if seen[i/2] == 0: del seen[i/2] elif 2*i in seen: seen[2*i] -= 1 if seen[2*i] == 0: del seen[2*i] else: if i in seen: seen[i] += 1 else: seen[i] = 1 return not bool(seen)
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: counter=Counter(A) for a in sorted(A, key=abs): if counter[a]==0: continue if counter[2*a]==0: return False counter[a]-=1 counter[2*a]-=1 return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
import collections class Solution: def canReorderDoubled(self, a: List[int]) -> bool: count=collections.Counter(a) for elem in sorted(a,key=abs): if count[elem]==0: continue if count[2*elem]==0: return False count[elem]-=1 count[2*elem]-=1 return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
from collections import defaultdict class Solution: def canReorderDoubled(self, A: List[int]) -> bool: if not A: return True count = 0 d = defaultdict(int) for i in A: d[i] += 1 for num in sorted(A): if num*2 in d and d[num] and d[num*2]: d[num] -= 1 d[num*2] -= 1 count += 1 return count == (len(A) // 2)
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: # Eliminate zero negative = [] positive = [] zero_numbers = 0 for a in A: if a < 0: negative.append(abs(a)) elif a > 0: positive.append(a) else: zero_numbers += 1 if zero_numbers % 2: return False negative.sort() positive.sort() # print(negative, positive) return self.helper(negative) and self.helper(positive) def helper(self, nums): while nums: cur_num = nums[0] idx = self.binarySearch(nums, 2 * cur_num) # print('helper',nums[0], idx) if idx == -1: return False nums.pop(idx) nums.pop(0) if len(nums) == 1: return False return True def binarySearch(self, nums, key): head = 0 tail = len(nums) - 1 # print(nums, key) while head <= tail: mid = (head + tail)//2 if nums[mid] > key: tail = mid - 1 elif nums[mid] < key: head = mid + 1 else: return mid return -1
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: count = collections.Counter(A) for a in sorted(count, key = lambda x: abs(x)): if count[a] > count[2 * a]: return False count[2 * a] -= count[a] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
import numpy as np class Solution: def canReorderDoubled(self, A: List[int]) -> bool: a = np.array(sorted(A)) non_pos_idxs = np.where(a >= 0)[0] if len(non_pos_idxs) > 0: non_pos_idx = non_pos_idxs[0] else: non_pos_idx = len(a) b = a[:non_pos_idx][::-1] a = a[non_pos_idx:] if len(b) % 2 != 0: return False if not self.check_doubled(a): return False if not self.check_doubled(b): return False return True def check_doubled(self, a): if len(a) == 0: return True count_dict = {} for val in a: if count_dict.get(val, 0) > 0: count_dict[val] -= 1 else: count_dict[2 * val] = count_dict.get(2 * val, 0) + 1 for val in list(count_dict.values()): if val > 0: return False return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: seen = {} A.sort() for ind,i in enumerate(A): #Logic for checking half and double of number if i/2 in seen: seen[i/2] -= 1 if seen[i/2] == 0: del seen[i/2] elif 2*i in seen: seen[2*i] -= 1 if seen[2*i] == 0: del seen[2*i] else: if i in seen: seen[i] += 1 else: seen[i] = 1 print(seen) return not bool(seen)
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: if not A: return True d=defaultdict(int) for x in A: d[x]+=1 n=len(A)//2 A.sort() for x in A: if d[x]>0: if 2*x in d and d[2*x]>0: d[2*x]-=1 d[x]-=1 n-=1 if n==0: return True #print(x,d,n) return False
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: A.sort() d = collections.defaultdict(int) for a in A: if d[2*a] > 0: d[2*a] -= 1 elif d[a/2] > 0: d[a/2] -= 1 else: d[a] += 1 return not any(d.values())
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
from collections import Counter class Solution: def canReorderDoubled(self, A: List[int]) -> bool: counts = [0]*(10**5+1) for a in A: if a >= 0: counts[a] += 1 for i in range(10**5+1): while counts[i] != 0: counts[i] -= 1 if counts[i] < 0: return False counts[2*i] -= 1 if counts[2*i] < 0: return False if not all(n == 0 for n in counts): return False for a in A: if a < 0: counts[-a] += 1 for i in range(10**5+1): while counts[i] != 0: counts[i] -= 1 if counts[i] < 0: return False counts[2*i] -= 1 if counts[2*i] < 0: return False if not all(n == 0 for n in counts): return False return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, arr: List[int]) -> bool: dicti=dict() for var in arr: if var in list(dicti.keys()): dicti[var]+=1 else: dicti[var]=1 arr.sort(key=lambda x: abs(x)) for var in arr: if dicti[var]==0: continue if(2*var not in list(dicti.keys())): return False if dicti[2*var]<=0: return False dicti[var]-=1 dicti[2*var]-=1 return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def get_result(self, sl, d): for k in sl: if d[k]==0: continue if 2*k not in d: return False v = d[k] v2 = d[2*k] if v>v2: return False d[k] -= v d[2*k] -= v return True def canReorderDoubled(self, A: List[int]) -> bool: d = dict() num_zeros = 0 for a in A: if a==0: num_zeros += 1 continue if a in d: d[a] += 1 else: d[a] = 1 neg = [k for k in list(d.keys()) if k<0] pos = [k for k in list(d.keys()) if k>0] if num_zeros%2!=0: return False sorted_pos = sorted(pos) sorted_neg = sorted(neg, reverse=True) res = self.get_result(sorted_pos, d) if not res: return False res = self.get_result(sorted_neg, d) if not res: return False return sum(list(d.values()))==0
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: cnt = collections.Counter(A) # It handles the case where there is odd number of 0, since the total number is even, there should be a number which cannot match because of the extra 0 for k in sorted(cnt, key=abs): if cnt[k] > cnt[2*k]: return False cnt[2*k] -= cnt[k] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
from collections import Counter class Solution: def canReorderDoubled(self, A: List[int]) -> bool: # Greedy # Time complexity: O(NlogN) # Space complexity: O(N) # count = Counter(A) # for x in sorted(A, key=abs): # if count[x] == 0: continue # if count[2 * x] == 0: return False # count[x] -= 1 # count[2 * x] -= 1 # return True count = Counter(A) for x in sorted(count, key=abs): if count[x] > count[2 * x]: return False count[2 * x] -= count[x] return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: count = collections.Counter(A) for i in sorted(count, key=abs): count[2*i] -= count[i] if count[2*i] < 0: return False return True
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: freq = dict() for i in A: freq[i] = freq.get(i, 0) + 1 for k in sorted(list(freq.keys()), key=lambda x: abs(x)): if freq[k] == 0: continue if freq[k] > freq.get(2*k, 0): return False freq[2*k] -= freq[k] return True