inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s: str) -> int:
c2nl = Counter('')
c2nr = Counter(s)
n = len(s)
ans = 0
for i in range(n-1):
c2nl[s[i]] += 1
c2nr[s[i]] -= 1
if c2nr[s[i]] == 0 : del c2nr[s[i]]
if len(c2nl)==len(c2nr): ans += 1
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
p = (int)
q = (int)
ll = len(s)
if ll < 2:
return 0
ns = len(set(s))
np = nq = 0
for ii in range(1,ll):
if len(set(s[:ii])) == ns:
np = ii
break
for ii in range(1,ll):
if len(set(s[ll-ii:])) == ns:
nq = ll-ii
break
if np <= nq:
return nq-np+1
ans = 0
for ii in range(1,ll):
p = len(set(s[:ii]))
q = len(set(s[ii:]))
if p == q:
ans += 1
elif p > q:
return ans
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
left = Counter()
right = Counter(s)
res = 0
for ch in s :
if right[ch] == 1:
del right[ch]
else:
right[ch] -= 1
left[ch] += 1
if len(list(left.keys())) == len(list(right.keys())):
res += 1
return res
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s: str) -> int:
s1 = Counter()
s2 = Counter(s)
count = 0
for a in s:
s1[a] += 1
s2[a] -= 1
if s2[a] == 0:
s2.pop(a)
if len(s1) == len(s2):
count += 1
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
dic_b ={}
for i in s[1:]:
if i not in dic_b:
dic_b[i] = 1
else:
dic_b[i]+=1
b_count = len(dic_b)
a= list(s[:1])
a_count =1
count = int(b_count==a_count)
ix=1
while ix<len(s):
if s[ix] not in a:
a_count+=1
a.append(s[ix])
dic_b[s[ix]]-=1
if dic_b[s[ix]] ==0:
b_count-=1
count+= int(a_count == b_count)
ix+=1
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
def increment_counter(counter, key):
counter[key] += 1
def decrement_counter(counter, key):
counter[key] -= 1
if counter[key] == 0:
del counter[key]
def naive_solution(s: str) -> int:
total = Counter(s)
count = 0
p = Counter()
for char in s:
p[char] += 1
q = total - p
if len(p) == len(q):
count += 1
return count
def optimized(s: str) -> int:
prefix = Counter() # initialize prefix as empty
suffix = Counter(s) # initialize suffix as total
count = 0
for char in s:
increment_counter(prefix, char)
decrement_counter(suffix, char)
if len(prefix) == len(suffix):
count += 1
return count
class Solution:
def numSplits(self, s: str) -> int:
# return naive_solution(s)
return optimized(s)
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
if len(s) < 2:
return 0
c = 0
left, right = defaultdict(int), defaultdict(int)
left[s[0]] = 1
for i in range(1,len(s)):
right[s[i]] +=1
if len(right) == 1:
c+=1
for i in range(1, len(s)):
right[s[i]]-=1
if right[s[i]] == 0:
right.pop(s[i])
left[s[i]]+=1
if len(left) == len(right):
c+=1
return c
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
def increment_counter(counter, key):
counter[key] += 1
def decrement_counter(counter, key):
if key in counter:
counter[key] -= 1
if counter[key] == 0:
del counter[key]
def naive_solution(s: str) -> int:
total = Counter(s)
count = 0
p = Counter()
for char in s:
p[char] += 1
q = total - p
if len(p) == len(q):
count += 1
return count
def optimized(s: str) -> int:
prefix = Counter() # initialize prefix as empty
suffix = Counter(s) # initialize suffix as total
count = 0
for char in s:
increment_counter(prefix, char)
decrement_counter(suffix, char)
if len(prefix) == len(suffix):
count += 1
return count
class Solution:
def numSplits(self, s: str) -> int:
# return naive_solution(s)
return optimized(s)
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s: str) -> int:
hist_left = Counter(s[:1])
hist_right = Counter(s[1:])
count = 0
for i in range(1, len(s)):
v = s[i]
if len(list(hist_left.keys())) == len(list(hist_right.keys())):
count += 1
hist_left[v] += 1
if hist_right[v] == 1:
del hist_right[v]
else:
hist_right[v] -= 1
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
right = collections.Counter(s)
left = collections.defaultdict(int)
res = 0
for i in range(len(s)-1):
c = s[i]
right[c] -= 1
if not right[c]: del right[c]
left[c] += 1
if len(left) == len(right): res += 1
return res
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
left_unique = [0 for x in range(len(s))]
right_unique = [0 for x in range(len(s))]
left_set = set()
right_set = set()
for x in range(len(s)):
if s[x] not in left_set:
left_set.add(s[x])
if x > 0:
left_unique[x] =left_unique[x-1] + 1
else:
left_unique[x] += 1
else:
left_unique[x] = left_unique[x-1]
if s[len(s)-1-x] not in right_set:
right_set.add(s[len(s)-x-1])
if x > 0:
right_unique[len(s)-x-1] = right_unique[len(s)-x] + 1
else:
right_unique[len(s)-x-1]+=1
else:
right_unique[len(s)-x-1] = right_unique[len(s)-x]
min_splits = 0
for x in range(1,len(s)):
if left_unique[x-1] == right_unique[x]:
min_splits+=1
return min_splits
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
fila = {}
tot = len(set(s))
x = set()
for i in range(len(s)):
x.add(s[i])
if len(x) == tot:
fa = i
break
x = set()
for i in range(len(s)-1, -1, -1):
x.add(s[i])
if len(x) == tot:
la = i
break
if fa <= la:
return la - fa
for i, c in enumerate(s):
if c not in fila:
fila[c] = [i, i]
else:
fila[c][1] = i
firsts, lasts = set(), set()
for a, b in fila.values():
firsts.add(a)
lasts.add(b)
ret = 0
cl, cr = 0, len(fila)
for i in range(len(s)-1):
if i in firsts:
cl += 1
if i in lasts:
cr -= 1
ret += int(cl == cr)
if cr < cl:
break
return ret
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
if len(s)==1:
return 0
ln=len(s)
ans=0
t1={}
t2={}
l1,l2=0,0
t1[s[0]]=1
for i in range(1,ln):
if s[i] in list(t2.keys()):
t2[s[i]]+=1
else:
t2[s[i]]=1
if len(list(t1.keys()))==len(list(t2.keys())):
ans+=1
for i in range(1,ln):
if s[i] in list(t1.keys()):
t1[s[i]]+=1
else:
t1[s[i]]=1
t2[s[i]]-=1
if t2[s[i]]==0:
del t2[s[i]]
if len(t1)==len(t2):
ans+=1
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def make_hist(self, array : str) -> dict:
hist = {}
for s in array:
if s not in list(hist.keys()):
hist[s] = 1
else:
hist[s] += 1
return hist
def numSplits(self, s: str) -> int:
hist_left = self.make_hist(s[:1])
hist_right = self.make_hist(s[1:])
count = 0
for i in range(1, len(s)):
v = s[i]
if len(list(hist_left.keys())) == len(list(hist_right.keys())):
count += 1
if v not in list(hist_left.keys()):
hist_left[v] = 1
else:
hist_left[v] += 1
if v in list(hist_right.keys()):
if hist_right[v] == 1:
del hist_right[v]
else:
hist_right[v] -= 1
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
left_map = {}
right_map = {}
unique_right = 0
unique_left = 0
for i in range(len(s)):
if s[i] not in right_map:
right_map[s[i]] = 1
unique_right += 1
else:
right_map[s[i]] += 1
good_split = 0
for i in range(len(s)):
# add to left map
if s[i] not in left_map:
left_map[s[i]] = 1
unique_left += 1
else:
left_map[s[i]] += 1
# remove from right map
if right_map[s[i]] == 1:
unique_right -= 1
right_map[s[i]] -= 1
if unique_left == unique_right:
good_split += 1
return good_split
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s: str) -> int:
rightS = Counter(s)
leftS = Counter()
count = 0
for c in s:
leftS[c]+=1
rightS[c]-=1
if rightS[c]==0:
del rightS[c]
count += (len(leftS.keys()) ==len(rightS.keys()))
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
# set up splitting at idx 1
p_dict = {s[0]: 1}
q_dict = {}
for char in s[1:]:
self.addToDict( q_dict, char )
if ( len(p_dict.keys()) == len(q_dict.keys()) ) :
count = 1
else:
count = 0
for i in range(1, len(s) - 1):
# find the char here
char = s[i]
# remove from q, add to p
self.removeFromDict( q_dict, char)
self.addToDict( p_dict, char )
if ( len(p_dict.keys()) == len(q_dict.keys()) ):
count += 1
return count
def addToDict( self, my_dict, value ):
if (my_dict.get(value) is None):
my_dict[value] = 1
else:
my_dict[value] += 1
def removeFromDict( self, my_dict, value ):
if (my_dict.get(value) == 1):
my_dict.pop(value)
elif (my_dict.get(value) > 1):
my_dict[value] -= 1
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
cur, h = Counter(), Counter(s)
cnt = 0
for c in s:
h[c] -= 1
cur[c] += 1
if h[c] == 0: del h[c]
cnt += len(h) == len(cur)
return cnt
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
if not s or len(s) < 0:
return 0
right = {}
left = {s[0]: 1}
count = 0
if len(left) == len(right):
count+= 1
for i in range(1, len(s)):
if s[i] in right:
right[s[i]] += 1
else:
right[s[i]] = 1
if len(left) == len(right):
count+= 1
for i in range(1, len(s)):
right[s[i]] -= 1
if right[s[i]] == 0:
del right[s[i]]
if s[i] in left:
left[s[i]] += 1
else:
left[s[i]] = 1
if len(left) == len(right):
count+= 1
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s: str) -> int:
l, r = {}, Counter(s)
ans = 0
for i in range(len(s)-1):
# Add s[i] to l
if s[i] in l:
l[s[i]] += 1
else:
l[s[i]] = 1
# Remove s[i] from r
if s[i] in r:
r[s[i]] -= 1
if r[s[i]] == 0:
del r[s[i]]
if len(l) == len(r):
#print(i, l, r)
ans += 1
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
c=Counter(s)
d=dict()
r=0
for i in s:
d[i]=1
if(i in c):
c[i]-=1
if(c[i]==0):
del c[i]
if(len(c)==len(d)):
r+=1
return r
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
p, q, ans = Counter(), Counter(s), 0
for c in s[:-1]:
p[c] += 1
q[c] -= 1
if not q[c]:
del q[c]
ans += len(p) == len(q)
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
left_count = Counter()
right_count = Counter(s)
res = 0
for c in s:
left_count[c] += 1
right_count[c] -= 1
if right_count[c] == 0:
del right_count[c]
if len(left_count) == len(right_count):
res += 1
return res
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
result1 = [[s[0]]]
result2 = [[s[-1]]]
n = len(s)
for i in range(1,len(s)):
list1 = list(result1[i-1])
list2 = list(result2[i-1])
# print(list1, list2)
if s[i] not in list1:
list1.append(s[i])
if s[n-1-i] not in list2:
list2.append(s[n-1-i])
result1.append(list1)
result2.append(list2)
result = 0
result2.reverse()
# print(result1)
# print(result2)
for i in range(len(s)-1):
if len(result1[i]) == len(result2[i+1]):
result += 1
return result
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s: str) -> int:
p, q, ans = Counter(), Counter(s), 0
for c in s[:-1]:
p[c] += 1
q[c] -= 1
if not q[c]:
del q[c]
ans += len(p) == len(q)
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
# stores
unique_chars = {}
for i, char in enumerate(s):
if char in unique_chars:
first, last = unique_chars[char]
unique_chars[char] = (first, i)
else:
unique_chars[char] = (i, i)
n_unique_chars = len(list(unique_chars.keys()))
events = []
for start, end in list(unique_chars.values()):
events.append((start, 1))
events.append((end, -1))
events.sort(key=lambda x: x[0])
left_num = 0
right_num = n_unique_chars
total = 0
start_good = None
#print(events)
for i, event_type in events:
#print(i, event_type, left_num, right_num)
if start_good is not None:
return i - start_good
if event_type == 1:
left_num += 1
else:
right_num -= 1
if left_num == right_num:
start_good = i
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
from collections import Counter
class Solution:
def numSplits(self, s):
res = 0
left = {}
right = Counter(s)
for i in range(len(s)):
left[s[i]] = left.get(s[i], 0) + 1
right[s[i]] -= 1
if right[s[i]] == 0:
del right[s[i]]
if len(left) == len(right):
res += 1
return res
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
if len(s) == 1:
return 0
lmap = collections.Counter(s[0:1])
rmap = collections.Counter(s[1:])
ans = 0
for i in range(1,len(s)):
if len(lmap) == len(rmap):
ans += 1
lmap.update([s[i]])
rmap[s[i]] -= 1
if not rmap[s[i]] :
del rmap[s[i]]
return ans
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
res = 0
# construct dict from 1 to last
# iterate from 1 and remove the elm from dict (if 0 then del elm from dict) and add it to the set
# check if leng are equal
fs = set()
d={}
for x in s:
if x not in d:
d[x]=1
else:
d[x]+=1
i = 0
while i<len(s):
fs.add(s[i])
if d[s[i]]>1:
d[s[i]]-=1
else:
del d[s[i]]
if len(fs)==len(set(d.keys())):
res+=1
i+=1
return res
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, string: str) -> int:
def isValid(string, i):
return 0 <= i < len(string)
def getLength(string, unique, i):
if isValid(string, i):
unique.add(string[i])
return len(unique)
return len(unique)
left, right = [0] * len(string), [0] * len(string)
left_unique, right_unique = set(), set()
reversed_string = string[::-1]
for i in range(len(left)):
left[i] = getLength(string, left_unique, i)
right[len(right)-1-i] = getLength(reversed_string, right_unique, i)
good_splits = 0
for i in range(len(left)-1):
if right[i+1] == left[i]:
good_splits += 1
return good_splits
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
if len(s) == 0:
return 0
count = 0
counterLeft = collections.Counter()
counterRight = collections.Counter(s)
for element in s:
counterLeft[element] += 1
counterRight[element] -= 1
if counterRight[element] == 0:
counterRight.pop(element)
if len(counterLeft) == len(counterRight):
count += 1
return count
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
if not s:
return 0
l_unique = set([])
r_unique = set(s)
good_splits = 0
for i, x in enumerate(s):
l_unique.add(x)
if x not in s[i+1:]:
r_unique.discard(x)
if len(l_unique) == len(r_unique):
good_splits += 1
return good_splits
|
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Example 3:
Input: s = "aaaaa"
Output: 4
Explanation: All possible splits are good.
Example 4:
Input: s = "acbadbaada"
Output: 2
Constraints:
s contains only lowercase English letters.
1 <= s.length <= 10^5
|
class Solution:
def numSplits(self, s: str) -> int:
result1 = [[s[0]]]
result2 = [[s[-1]]]
n = len(s)
for i in range(1,len(s)):
list1 = list(result1[i-1])
list2 = list(result2[i-1])
# print(list1, list2)
if s[i] not in list1:
list1.append(s[i])
if s[n-1-i] not in list2:
list2.append(s[n-1-i])
result1.append(list1)
result2.append(list2)
result = 0
# result2.reverse()
# print(result1)
# print(result2)
for i in range(len(s)-1):
if len(result1[i]) == len(result2[n-1-(i+1)]):
result += 1
return result
|
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
Division between two integers should truncate toward zero.
The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.
Example 1:
Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
|
class Solution:
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
s = []
for token in tokens:
if token == "+":
a = int(s.pop())
b = int(s.pop())
s.append(a+b)
elif token == "/":
a = int(s.pop())
b = int(s.pop())
s.append(b/a)
elif token == "*":
a = int(s.pop())
b = int(s.pop())
s.append(a*b)
elif token == "-":
a = int(s.pop())
b = int(s.pop())
s.append(b-a)
else:
s.append(token)
if len(s) is not 1:
return False
else:
return int(s.pop())
|
Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).
The returned string must have no leading zeroes, unless the string is "0".
Example 1:
Input: 2
Output: "110"
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2
Example 2:
Input: 3
Output: "111"
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3
Example 3:
Input: 4
Output: "100"
Explantion: (-2) ^ 2 = 4
Note:
0 <= N <= 10^9
|
class Solution:
def baseNeg2(self, N: int) -> str:
# res = []
# x = N
# while x:
# res.append(x & 1)
# x = -(x >> 1)
# return \"\".join(map(str, res[::-1] or [0]))
neg = [1 << i for i in range(1, 33, 2)]
for mask in neg:
if N & mask: N += mask*2
return bin(N)[2:]
|
Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).
The returned string must have no leading zeroes, unless the string is "0".
Example 1:
Input: 2
Output: "110"
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2
Example 2:
Input: 3
Output: "111"
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3
Example 3:
Input: 4
Output: "100"
Explantion: (-2) ^ 2 = 4
Note:
0 <= N <= 10^9
|
class Solution:
def baseNeg2(self, N):
res = []
while N != 0:
m = N %(-2)
if m == -1:
m = 1
N = (N-m) // (-2)
res.append(str(m))
return ''.join(res[::-1]) or '0'
|
Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).
The returned string must have no leading zeroes, unless the string is "0".
Example 1:
Input: 2
Output: "110"
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2
Example 2:
Input: 3
Output: "111"
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3
Example 3:
Input: 4
Output: "100"
Explantion: (-2) ^ 2 = 4
Note:
0 <= N <= 10^9
|
class Solution:
def baseNeg2(self, N: int) -> str:
return str(bin((0xAAAAAAAAAAAAAAAA+N)^0xAAAAAAAAAAAAAAAA))[2:]
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if nums.count(0) == len(nums):
return True
idx = nums.index(1)
ctr = 0
for num in nums[idx+1:]:
if num == 1:
if ctr < k:
return False
ctr = 0
else:
ctr+=1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
N = len(nums)
past, curr = -1, 0
for curr in range(N):
if nums[curr] == 1:
if past < 0:
past = curr
elif curr-past > k:
past = curr
else:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
s=-1
n=len(nums)
for i in range(n):
if(nums[i]==1 and s==-1):
s=i
elif(nums[i]==1):
interval=i-s-1
if(interval<k):
return False
s=i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
count = 0
first = False
for i in nums:
if i == 1 and not first:
first = True
elif i == 1 and first:
if k <= count:
count = 0
else:
return False
else:
count += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
counter = 0
bool = False
for i in nums:
if i == 1 and not bool:
bool = True
elif i == 1 and counter < k and bool:
return False
elif i == 1 and counter >= k and bool:
counter = 0
else:
counter += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
lis=[]
for i in range(len(nums)):
if nums[i]==1:
lis.append(i)
for i in range(1,len(lis)):
if (lis[i]-lis[i-1])-1<k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
zCounter = k
for n in nums:
if n==1:
if zCounter<k:
return False
zCounter = 0
else:
zCounter += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
p = 0
count = k + 1
while p < len(nums):
if nums[p] == 1:
if count <= k:
return False
else:
count = 0
count += 1
p += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
count = 0
for i in range(len(nums)):
if nums[i] == 1:
if i >= 1:
if count < k:
return False
count = 0
else:
count += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
s=-1
e=-1
n=len(nums)
for i in range(n):
if(nums[i]==1 and s==-1):
s=i
elif(nums[i]==1):
e=i
interval=e-s-1
if(interval<k):
return False
s=i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
pos = []
for i in range(len(nums)):
if nums[i] == 1:
pos.append(i)
true = 0
false = 0
for j in range(len(pos)-1):
if pos[j+1] - pos[j] <= k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if 1 not in nums:
return True
num_place=nums.index(1)
f_idx=nums.index(1)+1
for i in range(f_idx,len(nums)):
if nums[i] == 1 and i-num_place < k+1:
return False
elif nums[i] == 1 and i-num_place > k:
num_place=i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
next_one = 0
previous_one = 0
while(next_one < len(nums) and nums[next_one] != 1):
next_one += 1
if (next_one == len(nums)):
return True
previous_one = next_one
next_one += 1
while (next_one < len(nums)):
while (next_one < len(nums) and nums[next_one] != 1):
next_one += 1
if (next_one < len(nums) and next_one - previous_one - 1 < k):
return False
elif (next_one < len(nums) and next_one - previous_one - 1 >= k):
previous_one = next_one
else:
return True
next_one += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
zeros=0
i=0
while i<len(nums):
if i==0 and nums[i]==1:
i+=1
continue
if nums[i]==1 and zeros<k:
return False
break
elif nums[i]==0:
zeros+=1
i+=1
elif nums[i]==1 and zeros>=k:
zeros=0
i+=1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
prev=0
while prev<len(nums) and nums[prev]==0:
prev+=1
if prev==len(nums) or nums[prev]==0:
return True
if k==0:
return True
for i in range(prev+1,len(nums)):
if nums[i]==1:
if i-prev<=k:
return False
prev=i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if not k:
return True
pre_index = -1
for i, val in enumerate(nums):
if val:
if pre_index != -1 and i - pre_index - 1 < k:
return False
pre_index = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
# 8:23
def kLengthApart(self, nums: List[int], k: int) -> bool:
i = 0
while(i < len(nums) and nums[i] == 0):
i += 1
first = i
for j in range(i+1, len(nums)):
if nums[j] == 0:
continue
else:
second = j
print((first, second))
dis = second - first - 1
if dis < k:
return False
first = second
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
position = -1
for index, item in enumerate(nums):
if item & 1:
if index - position - 1 < k and position != -1:
return False
position = index
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
flag = 0
start = None
end = 0
for i in range(len(nums)):
if nums[i]==1 and flag ==0:
start = i
flag =1
elif nums[i] ==1 and flag==1:
end =i
dis = end - start
start = i
if dis <= k: return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
ind = 0
for i, num in enumerate(nums):
if not nums[ind] and num:
ind = i
if i != ind and num:
if (i - ind) < (k + 1):
return False
ind = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
locs = {}
for i in range(len(nums)):
if nums[i] == 1:
locs[i] = True
key= list(locs.keys())
for i in range(len(key)-1):
if key[i+1] - key[i] - 1 < k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
try:
last = nums.index(1)
except:
return True
for i in range(last + 1, len(nums)):
if nums[i] == 1 and i - last <= k:
return False
elif nums[i] == 1:
last = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
s = -1
for i in range(len(nums)):
if nums[i] == 1 and s == -1:
s = i
elif nums[i] == 1 and i-s <= k:
return False
elif nums[i] == 1 and i-s > k:
s = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
n=len(nums)
prev=-1
ans=[]
for i in range(n):
if prev==-1:
if nums[i]==1:
prev=i
else:
if nums[i]==1:
ans.append(i-prev-1)
prev=i
for i in ans:
if i<k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
xs = [i for i, n in enumerate(nums) if n]
return all(y-x > k for x,y in zip(xs, xs[1:]))
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
arr=[]
for i in range(len(nums)):
if(nums[i]==1):
arr.append(i)
# print(arr)
for i in range(1,len(arr)):
if(arr[i]-arr[i-1]-1<k):
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
import numpy as np
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
locs = np.where(np.array(nums) == 1)[0]
return np.all(np.abs(np.diff(locs)) > k)
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
temp = 0
increment = False
one_checked = False
for i in nums:
if one_checked and i != 1:
temp += 1
print(temp, i)
if i == 1:
if one_checked and temp < k:
return False
temp = 0
one_checked = True
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
import numpy as np
ones = np.nonzero(nums)[0]
if ones.size <= 1:
return True
diffs = np.diff(ones) - 1
return k <= np.min(diffs)
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
# import numpy as np
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
# locs = np.where(np.array(nums) == 1)[0]
# return np.all(np.diff(locs) > k)
# locs = [i for i in range(len(nums)) if nums[i] == 1]
# return all([locs[i+1] - locs[i] > k for i in range(len(locs)-1)])
# for i in range(len(locs)-1):
# if locs[i+1] - locs[i] <= k:
# return False
# return True
last_loc, curr_loc = -1, -1
for i, n in enumerate(nums):
if n == 1:
if last_loc == -1:
last_loc = i
elif curr_loc == -1:
curr_loc = i
curr_loc = i
print((i, last_loc, curr_loc))
if curr_loc - last_loc <= k and curr_loc != last_loc:
return False
last_loc = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
count = k
for num in nums:
if num == 1:
if count < k:
return False
count = 0
else:
count += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
from typing import List
import re
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
matches = re.finditer(r'(1)', ''.join(map(str, nums)))
indexes = tuple([getattr(el, 'span')()[0] for el in matches])
for i in range(1, len(indexes)):
diff = indexes[i] - indexes[i-1]
if diff <= k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
left = -1
for i in range(len(nums)):
if nums[i]==1:
if left ==-1:
left = i
else:
if i-left-1<k:
return False
else:
left = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
#find the first 1:
start = 0
for ind in range(len(nums)):
if nums[ind] == 1:
start = ind
break
#return true if no 1's in the array, or there's only one 1 in the last place
if start >= len(nums)-1: return True
minDist = float('inf') #represents infinity
print(start)
#check rest of the array:
for i in range(start+1, len(nums)):
if nums[i] == 1:
dist = i - start - 1
if dist < minDist:
minDist = dist
start = i
return minDist >= k
#make faster:
# enumerate(nums)
#nums.index(1) --> gets index of first 1 apparently
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
import numpy as np
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
# locs = np.where(np.array(nums) == 1)[0]
# return np.all(np.diff(locs) > k)
locs = [i for i in range(len(nums)) if nums[i] == 1]
return all([locs[i+1] - locs[i] > k for i in range(len(locs)-1)])
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
dis = k
for n in nums:
if n:
if dis<k:
return False
else:
dis=0
else:
dis+=1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
ones = [-k - 1]
for i, num in enumerate(nums):
if num:
if i - ones[-1] < k + 1:
return False
else:
ones.append(i)
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last = -k-1
for i in range(len(nums)):
if nums[i]:
if i-last>k:
last = i
else:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last = float('-inf')
for i, x in enumerate(nums):
if x==1:
if i-1-last<k:
return False
last = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
for idx in range(len(nums)):
if nums[idx] == 1 and 1 in nums[idx+1:idx+k+1]:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
# keep a list of indices of all the 1s
prev = -1
for i in range(len(nums)):
if nums[i] == 1:
if prev != -1 and i - prev -1 < k:
return False
prev = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
counter = -1
for num in nums:
if num == 1:
if counter == -1:
counter = 0
elif counter >= k:
counter = 0
else:
return False
elif counter != -1:
counter += 1
return True
0, 1, 0, 1
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
while sum(nums) >= 2:
temp = nums.index(1)
nums[temp] = 0
if nums.index(1) - temp <= k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
before = -k-5;
for i in range(len(nums)):
if nums[i] == 1:
if i-before <= k:
return False;
before = i;
return True;
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
idx = [n for n, x in enumerate(nums) if x == 1]
for i in range(1, len(idx)):
print((idx[i], idx[i-1]))
if idx[i] - idx[i-1] <= k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
def find(nums, *argc):
try:
return nums.index(*argc)
except ValueError:
return -1
prev = find(nums, 1)
while prev != -1:
cur = find(nums, 1, prev + 1)
if cur != -1 and (cur - prev - 1) < k:
return False
prev = cur
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
pos = 0
while pos<len(nums):
#print(nums[pos:])
try:
pos = nums.index(1,pos)
pos2 = nums.index(1,pos+1)
except:
break
#print(pos,pos2)
if pos2-pos-1<k:
return False
pos = pos2
print(pos)
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
#find the first 1:
start = 0
for ind in range(len(nums)):
if nums[ind] == 1:
start = ind
break
#return true if no 1's in the array, or there's only one 1 in the last place
if start >= len(nums)-1: return True
#improvements: can just use k, no need forfloat('inf') because k is the max possible distance
minDist = len(nums)
#check rest of the array:
for i in range(start+1, len(nums)):
if nums[i] == 1:
dist = i - start - 1
if dist < minDist:
minDist = dist
start = i
return minDist >= k
#make faster:
# enumerate(nums)
#nums.index(1) --> gets index of first 1 apparently
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
i, j, d= float('-inf'), 0, float('inf')
while j <len(nums):
if nums[j]:
d = min(d, j-i-1)
if d<k:
return False
i = j
j+=1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
counter = -1
for i in nums:
if counter < 0:
if i == 1:
counter = 0
else:
if i == 0:
counter += 1
else:
if counter < k:
return False
counter = 0
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last1 = -1
for num in nums:
# See 0, have seen 1
if num == 0 and last1 != -1:
last1 += 1
elif num == 1 and last1 != -1:
# Check against k, can return False
if last1 < k:
return False
last1 = 0
elif num == 1 and last1 == -1:
last1 = 0
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
i = float('-inf')
for j, x in enumerate(nums):
if x:
if j - i <= k:
return False
i = j
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
start = 0
while start < len(nums) and nums[start] != 1:
start += 1
for i in range(start + 1, len(nums)):
if nums[i] == 1:
if i - start - 1 < k:
return False
else:
start = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
distance = k
for num in nums:
if num:
if distance < k:
return False
distance = 0
else:
distance = distance + 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
prev = 0
i = 0
for num in nums:
if num == 1:
if prev and k > i - prev:
return False
prev = i + 1
i += 1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last1 = -1
tally = len(nums)
# print(last1)
for idx, val in enumerate(nums):
if val == 1:
last1 = idx
if tally < k:
return False
tally = 0
else:
tally+=1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if nums.count(1)==0:
return True
c=[]
for i in range(len(nums)):
if nums[i]==1:
c.append(i)
for i in range(1,len(c)):
if c[i]-c[i-1]<=k:
return False
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
#find the first 1:
start = 0
while start < len(nums):
if nums[start] == 0:
start += 1
else:
break
if start >= len(nums)-1: return True #return true if no 1's in the array, or there's only one 1 in the last place
minDist = float('inf') #represents infinity
print(start)
#check rest of the array:
for i in range(start+1, len(nums)):
if nums[i] == 1:
dist = i - start - 1
if dist < minDist:
minDist = dist
start = i
return minDist >= k
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
c=nums.count(1)-1
if c<0:
return True
if c==0:
return True
ini=nums.index(1)
while c:
fin=nums.index(1,ini+1,len(nums))
print(fin)
if fin-ini<(k+1):
return False
c-=1
ini=fin
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last_one = None
for i in range(len(nums)):
if nums[i]:
if last_one != None and i - last_one - 1 < k:
return False
last_one = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart1(self, nums: List[int], k: int) -> bool:
d = [i for i,v in enumerate(nums) if v == 1]
#print(d)
for i in range(1,len(d)):
if d[i]-d[i-1] <= k:
return False
return True
def kLengthApart(self, nums: List[int], k: int) -> bool:
idx = -1
for i in range(len(nums)):
if nums[i] == 1:
if idx != -1 and i - idx - 1 < k:
return False
idx = i
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
# Loop through the array with 2 pointers at n and n+1
# Take some action depending on the 4 cases
if len(nums) == 1:
return True
# Preprocess all the leading zeros
i = 0
while i < len(nums):
if nums[i] == 1:
break
i += 1
counter = 0
i += 1
while i < len(nums):
j = i-1
print(nums[j], nums[i])
# 10 -> increment counter
# 00 -> increment counter
# 01 -> check >= k, reset counter
# 11 -> skip
if nums[j] == 0 and nums[i] == 1:
if counter < k:
return False
counter = 0
if (nums[j] == 1 or nums[j] == 0) and nums[i] == 0:
counter += 1
if nums[j] == 1 and nums[i] == 1 and k >= 1:
return False
i+=1
j+=1
return True
|
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1
|
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
p = -1000000
n = len(nums)
for i in range(n):
if nums[i] == 1:
if i-p-1 >= k:
p = i
else:
return False
return True
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.