post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1520004/99.7-Python-3-solution-with-17-lines-no-search-explained
class Solution: def numSplits(self, s: str) -> int: # this is not neccessary, but speeds things up length = len(s) if length == 1: # never splittable return 0 elif length == 2: # always splittable return 1 # we are recording the first and last occurence of each included letter first = {} # max size = 26 last = {} # max size = 26 for index, character in enumerate(s): # O(n) if character not in first: first[character] = index last[character] = index # we are concatenating the collected indices into a list and sort them indices = list(first.values()) + list(last.values()) # max length 26 + 26 = 52 indices.sort() # sorting is constant O(1) because of the length limit above # all possible splits will be in the middle of this list middle = len(indices)//2 # always an integer because indices has an even length # there are this many possible splits between the two 'median' numbers return indices[middle] - indices[middle-1]
number-of-good-ways-to-split-a-string
99.7% Python 3 solution with 17 lines, no search, explained
epistoteles
70
2,200
number of good ways to split a string
1,525
0.694
Medium
22,600
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1636773/Python3-Simple-O(n)-solution-using-2-loops-with-set
class Solution: def numSplits(self, s: str) -> int: ans = 0 leftSet, leftCount = set(), [] for idx, ch in enumerate(s): leftSet.add(ch) leftCount.append(len(leftSet)) rightSet = set() for idx in range(len(s)-1, 0, -1): rightSet.add(s[idx]) if len(rightSet) == leftCount[idx-1]: ans += 1 return ans
number-of-good-ways-to-split-a-string
[Python3] Simple O(n) solution using 2 loops with set
freetochoose
2
211
number of good ways to split a string
1,525
0.694
Medium
22,601
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1934206/Python-O(n)-sokution-with-reverse-count
class Solution: def numSplits(self, s: str) -> int: n = len(s) suffixes = [0] * n unique = set() for i in reversed(range(n)): unique.add(s[i]) suffixes[i] = len(unique) counter = 0 unique = set() for i in range(n - 1): unique.add(s[i]) if len(unique) == suffixes[i+1]: counter += 1 return counter
number-of-good-ways-to-split-a-string
Python O(n) sokution with reverse count
fstar1
1
157
number of good ways to split a string
1,525
0.694
Medium
22,602
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1579968/Python3-or-O(n)-or-Faster-%3A-91.95-or-Simple-Solution
class Solution: def numSplits(self, s: str) -> int: prefix = [0]*len(s) suffix = [0]*len(s) unique = set() ans = 0 for index in range(len(s)): unique.add(s[index]) prefix[index] = len(unique) unique.clear() for index in range(len(s)-1, -1, -1): unique.add(s[index]) suffix[index] = len(unique) for index in range(len(s)-1): if prefix[index] == suffix[index+1]: ans += 1 return ans
number-of-good-ways-to-split-a-string
Python3 | O(n) | Faster : 91.95 | Simple Solution
Call-Me-AJ
1
188
number of good ways to split a string
1,525
0.694
Medium
22,603
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1538253/Python3-2-set
class Solution: def numSplits(self, s: str) -> int: """ index 0 1 2 3 value a. b. a c prefix a ab aba abac sufix. c ca. cab caba prelen 1 2 2 suflen. 1 2 3 """ prefixSet = set() sufixSet = set() prelen =[] suflen = [] n = len(s) for i in range(n): prefixSet.add(s[i]) sufixSet.add(s[n - i - 1]) prelen.append(len(prefixSet)) suflen.append(len(sufixSet)) print(prelen,suflen ) count = 0 for i in range(n - 1): if prelen[i] == suflen[n-i-2]: count += 1 return count
number-of-good-ways-to-split-a-string
[Python3] 2 set
zhanweiting
1
122
number of good ways to split a string
1,525
0.694
Medium
22,604
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1455861/Python-oror-Easy-Solution-oror-Dictionary
class Solution def numSplits(self, s: str) -> int: c = collections.Counter(s) d = dict() count = 0 for i in s: c[i] -= 1 if c[i] == 0: c.pop(i) try: d[i] += 1 except: d[i] = 1 if len(c) == len(d): count += 1 return (count)
number-of-good-ways-to-split-a-string
Python || Easy Solution || Dictionary
naveenrathore
1
143
number of good ways to split a string
1,525
0.694
Medium
22,605
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/2845673/in-n-time-complexity
class Solution: def numSplits(self, s: str) -> int: dc=defaultdict(lambda:0) dr=0 for a in s: dc[a]+=1 if(dc[a]==1): dr+=1 print(dr) print(dc) dl=0 dc2=defaultdict(lambda:0) ans=0 for a in s: dc2[a]+=1 dc[a]-=1 if(dc2[a]==1): dl+=1 if(dc[a]==0): dr-=1 print(dl,dr) if(dr==dl): ans+=1 return ans
number-of-good-ways-to-split-a-string
in n time complexity
droj
0
1
number of good ways to split a string
1,525
0.694
Medium
22,606
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/2841610/Python-90-faster-oror-O(N)-Solution
class Solution: def getNonZeroChars(self, arr): count = 0 for i in arr: if i > 0: count += 1 return count def numSplits(self, s: str) -> int: n_good = 0 chr_bits = [0]*26 for ch in s: chr_bits[ord(ch)-97] += 1 total_count = self.getNonZeroChars(chr_bits) curr_chr_set = set() for ch in s: curr_chr_set.add(ch) chr_bits[ord(ch)-97] -= 1 if (chr_bits[ord(ch)-97] == 0): total_count -= 1 if(len(curr_chr_set) == total_count): n_good += 1 return n_good
number-of-good-ways-to-split-a-string
Python 90% faster || O(N) Solution
amanjhurani5
0
2
number of good ways to split a string
1,525
0.694
Medium
22,607
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/2747658/Python3-or-O(n)-Solution
class Solution: def numSplits(self, s: str) -> int: ans = 0 freqDict = defaultdict(int) set1 = set(); set2 = set() for ch in s: freqDict[ch]+=1 set2.add(ch) for ch in s: set1.add(ch) freqDict[ch]-=1 if(freqDict[ch]==0): set2.remove(ch) if(len(set1)==len(set2)): ans+=1 return ans
number-of-good-ways-to-split-a-string
Python3 | O(n) Solution
ty2134029
0
4
number of good ways to split a string
1,525
0.694
Medium
22,608
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/2747138/prefix-and-suffix-sum
class Solution: def numSplits(self, s: str) -> int: n =len(s) pf =[0]*(n+1) sf=[0]*(n+1) s1=set() s2=set() for i in range(n): if s[i]in s1: c=0 else: c=1 pf[i+1]=pf[i]+c s1.add(s[i]) for i in range(n-1,-1,-1): if s[i] in s2: c=0 else: c=1 sf[i]=sf[i+1]+c s2.add(s[i]) #print(pf) #print(sf) ans=0 for i in range(0,n+1): if pf[i]==sf[i]: ans+=1 return ans
number-of-good-ways-to-split-a-string
prefix and suffix sum
abhayCodes
0
10
number of good ways to split a string
1,525
0.694
Medium
22,609
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/2744328/Python-O(n)-Simple-Solution-using-Sets
class Solution: def numSplits(self, s: str) -> int: N = len(s) leftCtr = set() rightCtr = set() leftCounters = [] rightCounters = [] for i in range(N): c = s[i] leftCtr.add(c) leftCounters.append(len(leftCtr)) j = N-i-1 c = s[j] rightCtr.add(c) rightCounters.append(len(rightCtr)) good_splits = 0 for i in range(1,N): lc = leftCounters[i-1] rc = rightCounters[N-i-1] if lc == rc: good_splits += 1 return good_splits
number-of-good-ways-to-split-a-string
[Python] O(n) Simple Solution using Sets
koff82
0
2
number of good ways to split a string
1,525
0.694
Medium
22,610
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/2714729/Python3-Clear-solution-using-dicts-low-memory-explained
class Solution: def numSplits(self, s: str) -> int: output = 0 # Use two dicts to count occurances of characters on the left and right side of the split # Initially, the left is empty and the right contains all the letters dleft = {} dright = {} for char in s: dright[char] = dright.get(char, 0) + 1 # Then loop through each character in s, adding its occurance to the left dictionary and removing it # from the right. Then compare the length of the sets of keys of the dictionary (which counts distinct # characters) and increment `output` if they are equal. for i in range(len(s)): dleft[s[i]] = dleft.get(s[i], 0) + 1 if dright[s[i]] == 1: del dright[s[i]] else: dright[s[i]] = dright.get(s[i]) - 1 if len(dleft.keys()) == len(dright.keys()): output += 1 return output
number-of-good-ways-to-split-a-string
[Python3] Clear solution using dicts - low memory - explained
connorthecrowe
0
1
number of good ways to split a string
1,525
0.694
Medium
22,611
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1655052/Python3-or-Time-O(n)-or-Space-O(n)-or-Suffix-Prefix-Method
class Solution: def numSplits(self, s: str) -> int: n=len(s) left=set() right=set() pre=[0 for i in range(n)] post=[0 for i in range(n)] left.add(s[0]) pre[0]=1 for i in range(1,n): if s[i] not in left: pre[i]=1+pre[i-1] left.add(s[i]) else: pre[i]=pre[i-1] right.add(s[n-1]) post[n-1]=1 for j in range(n-2,-1,-1): if s[j] not in right: post[j]=1+post[j+1] right.add(s[j]) else: post[j]=post[j+1] ans=0 for i in range(n-1): if pre[i]==post[i+1]: ans+=1 return ans
number-of-good-ways-to-split-a-string
Python3 | Time- O(n) | Space- O(n) | Suffix-Prefix Method
Rohit_Patil
0
91
number of good ways to split a string
1,525
0.694
Medium
22,612
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1654261/Python-simple-solution-with-hashmap-only-No-DP
class Solution: def numSplits(self, s: str) -> int: n = len(s) left, right = defaultdict(int), defaultdict(int) res = 0 for c in s: right[c] += 1 for i in range(n): c = s[i] left[c] += 1 right[c] -= 1 if right[c] == 0: del right[c] if len(left) == len(right): res += 1 return res
number-of-good-ways-to-split-a-string
Python simple solution with hashmap only, No DP
byuns9334
0
101
number of good ways to split a string
1,525
0.694
Medium
22,613
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1553774/Py3Py-Simple-solution-using-two-dictionaries-w-comments
class Solution: def numSplits(self, s: str) -> int: # Init left = dict() right = dict() good = 0 # Fill the right dictionary with all chars for c in s: if c in right: right[c] += 1 else: right[c] = 1 # For each split, add the current char c to left # and remove the current char c from right for c in s: # Add to left if c in left: left[c] += 1 else: left[c] = 1 # remove from right if c in right: right[c] -= 1 if right[c] == 0: right.pop(c,None) # if length of both dictionary is same # then its a good split if len(left) == len(right): good += 1 return good
number-of-good-ways-to-split-a-string
[Py3/Py] Simple solution using two dictionaries w/ comments
ssshukla26
0
97
number of good ways to split a string
1,525
0.694
Medium
22,614
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1498884/Python3-Easy-to-follow-No-DP-%3Ap
class Solution: def numSplits(self, s: str) -> int: entire = {} for ind, c in enumerate(s): if(c not in entire): entire[c] = 0 entire[c] += 1 left = {} ans = 0 for ind, c in enumerate(s): if(c not in left): left[c] = 0 left[c] += 1 entire[c] -= 1 if(entire[c] == 0): del entire[c] if(len(left) == len(entire)): ans += 1 return ans
number-of-good-ways-to-split-a-string
[Python3] Easy to follow - No DP :p
vs152
0
122
number of good ways to split a string
1,525
0.694
Medium
22,615
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1383604/Python-or-Split-Technique-using-Hashmaps
class Solution: def numSplits(self, s: str) -> int: h1={} h2={} find={} dist=0 cnt=0 for k in range(1,len(s)): if s[k-1] not in find: h1[k]=dist+1 dist=h1[k] find[s[k-1]]="p" else: h1[k]=dist find={} dist=0 for k in range(len(s)-1,0,-1): if s[k] not in find: h2[k]=dist+1 dist=h2[k] find[s[k]]="p" else: h2[k]=dist for i,j in h1.items(): if j==h2[i]: cnt+=1 return cnt
number-of-good-ways-to-split-a-string
Python | Split Technique using Hashmaps
swapnilsingh421
0
79
number of good ways to split a string
1,525
0.694
Medium
22,616
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1210358/Python-Brute-Force-and-AC-Solution
class Solution: def numSplits(self, s: str) -> int: result, key = 0, set(s) for idx in range(1, len(s)): a = len(set(s[:idx]) & key) b = len(set(s[idx:]) & key) if a == b: result += 1 return result
number-of-good-ways-to-split-a-string
Python Brute Force & AC Solution
dev-josh
0
148
number of good ways to split a string
1,525
0.694
Medium
22,617
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1188720/Python3-simple-solution-using-two-dictionary
class Solution: def numSplits(self, s: str) -> int: d1 = {} d2 = {} for i in s: d2[i] = d2.get(i,0) + 1 count = 0 for i in s: x = list(d2.values()) if len(d1) == len(x) - (x.count(0)): count += 1 elif len(d1) > (len(x) - (x.count(0))): return count d1[i] = d1.get(i,0) + 1 d2[i] -= 1 return count
number-of-good-ways-to-split-a-string
Python3 simple solution using two dictionary
EklavyaJoshi
0
102
number of good ways to split a string
1,525
0.694
Medium
22,618
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/1105151/Python3-freq-table
class Solution: def numSplits(self, s: str) -> int: freq = {} for c in s: freq[c] = 1 + freq.get(c, 0) ans = 0 seen = set() for i, c in enumerate(s): seen.add(c) freq[c] -= 1 if not freq[c]: freq.pop(c) if len(seen) == len(freq): ans += 1 return ans
number-of-good-ways-to-split-a-string
[Python3] freq table
ye15
0
142
number of good ways to split a string
1,525
0.694
Medium
22,619
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/959713/Python3-Solution-Using-two-Hashmaps
class Solution: #Function for checking number of different characters in both hashmap. if both are same return True else False def compare(self): dist1,dist2 = 0,0 for key in self.first.keys(): if key not in self.second: dist1 += 1 for key in self.second.keys(): if key not in self.first: dist2 += 1 return True if dist1 == dist2 else False def numSplits(self, s: str) -> int: if len(s) == 1: return 0 count = 0 self.first = {} self.second = {} for i in range(len(s)): if(not self.first and not self.second): self.first = collections.Counter(s[:i+1]) self.second = collections.Counter(s[i+1:]) else: if s[i] in self.first: self.first[s[i]] = self.first[s[i]] + 1 else: self.first[s[i]] = 1 if self.second[s[i]] is not None and self.second[s[i]] <= 1: self.second.pop(s[i]) else: self.second[s[i]] -= 1 check = self.compare() if check: count += 1 return count
number-of-good-ways-to-split-a-string
Python3 Solution Using two Hashmaps
swap2001
0
99
number of good ways to split a string
1,525
0.694
Medium
22,620
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/796107/Python3-2-counters-Number-of-Good-Ways-to-Split-a-String
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
number-of-good-ways-to-split-a-string
Python3 2 counters - Number of Good Ways to Split a String
r0bertz
0
217
number of good ways to split a string
1,525
0.694
Medium
22,621
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/785659/Python3-using-dictionary
class Solution: def numSplits(self, s: str) -> int: ans=0 complete_dict = collections.Counter(s) curr_dict = collections.defaultdict(int) for i in range(len(s)-1): a = s[i] curr_dict[a] += 1 complete_dict[a] -= 1 if complete_dict[a] == 0: del complete_dict[a] if len(curr_dict.keys())==len(complete_dict.keys()): ans+=1 return ans
number-of-good-ways-to-split-a-string
Python3 using dictionary
harshitCode13
0
100
number of good ways to split a string
1,525
0.694
Medium
22,622
https://leetcode.com/problems/number-of-good-ways-to-split-a-string/discuss/756357/Intuitive-approach-by-two-dict-objects
class Solution: def numSplits(self, s: str) -> int: tail_d, head_d, ans = {}, {}, 0 def add_c(d, c): if c not in d: d[c] = 1 else: d[c] += 1 def remove_c(d, c): n = d[c] n -= 1 if n == 0: del d[c] else: d[c] = n def is_same(d1, d2): return len(d1) == len(d2) # 0) Initialize tail_d for c in s: add_c(tail_d, c) # 1) Start searching number of good ways of split for c in s: remove_c(tail_d, c) add_c(head_d, c) if is_same(tail_d, head_d): ans += 1 if len(tail_d) < len(head_d): break # 3) Return the answer return ans
number-of-good-ways-to-split-a-string
Intuitive approach by two dict objects
puremonkey2001
0
46
number of good ways to split a string
1,525
0.694
Medium
22,623
https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/discuss/1589995/Python3-O(n)-time-O(1)-space-solution
class Solution: def minNumberOperations(self, target: List[int]) -> int: res = target[0] for i in range(1, len(target)): if target[i] >= target[i - 1]: res -= target[i - 1] res += target[i] return res
minimum-number-of-increments-on-subarrays-to-form-a-target-array
[Python3] O(n) time, O(1) space solution
maosipov11
1
87
minimum number of increments on subarrays to form a target array
1,526
0.686
Hard
22,624
https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/discuss/2611547/Python3-List-Comprehension-1-line-O(n)
class Solution: def minNumberOperations(self, target: List[int]) -> int: return target[0] + sum([max(0, target[i + 1] - target[i]) for i in range(len(target) - 1)])
minimum-number-of-increments-on-subarrays-to-form-a-target-array
[Python3] List Comprehension, 1 line, O(n)
Unwise
0
7
minimum number of increments on subarrays to form a target array
1,526
0.686
Hard
22,625
https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/discuss/1481200/o(n)-or-Simple-Pattern
class Solution: def minNumberOperations(self, target: List[int]) -> int: threshold = 0 n = len(target) target.append(0) target.insert(0,0) ans =0 for i in range(1,n+1): if target[i-1]<=target[i]>target[i+1]: # print(target[i], threshold) ans += max(0,(target[i]-threshold)) elif target[i-1] > target[i] <= target[i+1]: threshold = target[i] return ans
minimum-number-of-increments-on-subarrays-to-form-a-target-array
o(n) | Simple Pattern
Sanjaychandak95
0
141
minimum number of increments on subarrays to form a target array
1,526
0.686
Hard
22,626
https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/discuss/1105152/Python3-mono-stack
class Solution: def minNumberOperations(self, target: List[int]) -> int: ans = 0 stack = [] # increasing stack for x in target: while stack and stack[-1] >= x: ans += stack.pop() - max(x, (stack or [0])[-1]) stack.append(x) prev = 0 for x in stack: ans += x - prev prev = x return ans
minimum-number-of-increments-on-subarrays-to-form-a-target-array
[Python3] mono-stack
ye15
0
98
minimum number of increments on subarrays to form a target array
1,526
0.686
Hard
22,627
https://leetcode.com/problems/shuffle-string/discuss/768482/Simple-Python-Solution-Faster-than-99.56
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) for i in range(len(s)): res[indices[i]] = s[i] return ''.join(i for i in res)
shuffle-string
Simple Python Solution - Faster than 99.56%
parkershamblin
25
4,200
shuffle string
1,528
0.857
Easy
22,628
https://leetcode.com/problems/shuffle-string/discuss/1273696/or-Python-3-or-94.31-faster-or-98.73-less-or
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: result = [""] * len(s) for i, letter in enumerate(s): result[indices[i]] = letter return "".join(result)
shuffle-string
| Python 3 | 94.31% faster | 98.73% less |
anotherprogramer
13
1,300
shuffle string
1,528
0.857
Easy
22,629
https://leetcode.com/problems/shuffle-string/discuss/2269941/PYTHON-3-Self-Understanding-or-Simple
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) for i in range(len(s)): res[indices[i]] = s[i] return ''.join(i for i in res)
shuffle-string
[PYTHON 3] Self Understanding | Simple
omkarxpatel
7
399
shuffle string
1,528
0.857
Easy
22,630
https://leetcode.com/problems/shuffle-string/discuss/2332794/Python-Simplest-Solution-With-Explanation-or-Beg-to-adv-or-String
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: ans = "" # taking empty string to save result for i in range(len(indices)): # loop for traversing ans += s[indices.index(i)] # firstly we`ll get the index of "i", and then we use that to have the char in the string. I.E. indices.index(0) = 4 ,s[4] = L return ans # giving out the answer.
shuffle-string
Python Simplest Solution With Explanation | Beg to adv | String
rlakshay14
5
328
shuffle string
1,528
0.857
Easy
22,631
https://leetcode.com/problems/shuffle-string/discuss/755851/Python3-fill-in-an-array
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: ans = [""]*len(s) for i, x in zip(indices, s): ans[i] = x return "".join(ans)
shuffle-string
[Python3] fill in an array
ye15
5
900
shuffle string
1,528
0.857
Easy
22,632
https://leetcode.com/problems/shuffle-string/discuss/755851/Python3-fill-in-an-array
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: return "".join(x for _, x in sorted(zip(indices, s)))
shuffle-string
[Python3] fill in an array
ye15
5
900
shuffle string
1,528
0.857
Easy
22,633
https://leetcode.com/problems/shuffle-string/discuss/1680681/Python-solution-36ms-Faster-than-91.56
class Solution(object): def restoreString(self, s, indices): y = [s[indices.index(i)] for i in range(len(indices))] return ("".join(y))
shuffle-string
Python solution - 36ms Faster than 91.56%
chikara1
4
459
shuffle string
1,528
0.857
Easy
22,634
https://leetcode.com/problems/shuffle-string/discuss/1884715/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution: def restoreString(self, s, indices): ans = [None] * len(s) for i,c in enumerate(s): ans[indices[i]] = c return "".join(ans)
shuffle-string
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
2
216
shuffle string
1,528
0.857
Easy
22,635
https://leetcode.com/problems/shuffle-string/discuss/1884715/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution: def restoreString(self, s, indices): s, n = list(s), len(s) for x in range(n): i, c = indices[x], s[x] while i != indices[i]: s[i], c = c, s[i] indices[i], i = i, indices[i] return "".join(s)
shuffle-string
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
2
216
shuffle string
1,528
0.857
Easy
22,636
https://leetcode.com/problems/shuffle-string/discuss/1884715/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution: def restoreString(self, s, indices): return "".join([c for i,c in sorted(zip(indices,s))])
shuffle-string
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
2
216
shuffle string
1,528
0.857
Easy
22,637
https://leetcode.com/problems/shuffle-string/discuss/1884715/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution: def restoreString(self, s, indices): arr = [None] * len(s) for i,v in enumerate(indices): arr[v] = i return "".join([s[arr[i]] for i in range(len(s))])
shuffle-string
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
2
216
shuffle string
1,528
0.857
Easy
22,638
https://leetcode.com/problems/shuffle-string/discuss/1884715/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution: def restoreString(self, s, indices): return "".join([s[indices.index(i)] for i in range(len(s))])
shuffle-string
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
2
216
shuffle string
1,528
0.857
Easy
22,639
https://leetcode.com/problems/shuffle-string/discuss/1548596/THE-SOLUTION-faster-than-99-python3
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: l = len(s) newStr = [''] * l for i in range(l): newStr[indices[i]] = s[i] return ''.join(newStr)
shuffle-string
THE SOLUTION - faster than 99% #python3
zoharfran
2
256
shuffle string
1,528
0.857
Easy
22,640
https://leetcode.com/problems/shuffle-string/discuss/755974/Python-1-liner
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: return ''.join([i[1] for i in sorted(zip(indices, s))])
shuffle-string
Python 1-liner
idontknoooo
2
166
shuffle string
1,528
0.857
Easy
22,641
https://leetcode.com/problems/shuffle-string/discuss/2641766/Python-Solution-or-Dictionary-or
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: Edict = {} for i in range(len(indices)): for j in range(len(s)): if i == j: Edict.update({indices[i]:s[j]}) # print(Edict) sortedList = sorted(Edict.items()) finalstring ="" for i in sortedList: finalstring = finalstring + i[1] return finalstring
shuffle-string
Python Solution | Dictionary |
Gaurav_Phatkare
1
335
shuffle string
1,528
0.857
Easy
22,642
https://leetcode.com/problems/shuffle-string/discuss/2445144/Python-Simple-Problem-(-85-Fast-)
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: i,l = indices , [] m = zip(i,s) q = sorted(set(m)) for j in q: l.append(j[1]) return "".join(l)
shuffle-string
Python Simple Problem ( 85% Fast )
SouravSingh49
1
170
shuffle string
1,528
0.857
Easy
22,643
https://leetcode.com/problems/shuffle-string/discuss/2264326/Python3-Runtime%3A-89ms-47.12ms-oror-Memory%3A-14mb-15.50
class Solution: # O(n+m) it should be O(N)? || O(n) # Runtime: 89ms 47.12ms || Memory: 14mb 15.50% def restoreString(self, s: str, indices: List[int]) -> str: newString = [0] * len(indices) for val, idx in zip(s, indices): newString[idx % len(s)] = val return ''.join(newString)
shuffle-string
Python3 # Runtime: 89ms 47.12ms || Memory: 14mb 15.50%
arshergon
1
66
shuffle string
1,528
0.857
Easy
22,644
https://leetcode.com/problems/shuffle-string/discuss/2015742/Python-Simple-Solution-Beats-~95
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) for pos, c in zip(indices, s): res[pos] = c return ''.join(res)
shuffle-string
Python Simple Solution - Beats ~95%
constantine786
1
170
shuffle string
1,528
0.857
Easy
22,645
https://leetcode.com/problems/shuffle-string/discuss/1757006/1528-shuffle-string-2-easy-ways
class Solution(object): def restoreString(self, s, indices): my_dict = {} string_literal = "" j=0 for each in indices: my_dict[each] = s[j] j += 1 for i in range(len(s)): string_literal += my_dict.get(i) return string_literal
shuffle-string
1528 - shuffle string - 2 easy ways
ankit61d
1
92
shuffle string
1,528
0.857
Easy
22,646
https://leetcode.com/problems/shuffle-string/discuss/1757006/1528-shuffle-string-2-easy-ways
class Solution(object): def restoreString(self, s, indices): string_literal_from_list = [""]*len(s) for i in range(len(s)): string_literal_from_list[indices[i]]=s[i] return ''.join(string_literal_from_list)
shuffle-string
1528 - shuffle string - 2 easy ways
ankit61d
1
92
shuffle string
1,528
0.857
Easy
22,647
https://leetcode.com/problems/shuffle-string/discuss/1273324/Python-using-dictionary.-Runtime%3A-52-ms-faster-than-82.25.
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: dic = {} word = "" for i in range(len(s)): dic[indices[i]] = s[i] for i in range(len(s)): word += dic[i] return word
shuffle-string
Python using dictionary. Runtime: 52 ms, faster than 82.25%.
calen-mcnickles
1
128
shuffle string
1,528
0.857
Easy
22,648
https://leetcode.com/problems/shuffle-string/discuss/1218478/Python-3-using-enumerate
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: temp_indicies = ['' for i in range(len(indices))] for i,j in enumerate(indices): temp_indicies[j] = s[i] return ''.join(temp_indicies)
shuffle-string
Python 3 using enumerate
dheeraj_alim
1
139
shuffle string
1,528
0.857
Easy
22,649
https://leetcode.com/problems/shuffle-string/discuss/1199501/Python-3-Easiest-With-Explanation-97.95-Faster
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: new_list = [None]*len(s) for i in range(len(indices)): new_list[indices[i]]=s[i] return ''.join(new_list)
shuffle-string
Python 3, Easiest, With Explanation, 97.95% Faster
unKNOWN-G
1
651
shuffle string
1,528
0.857
Easy
22,650
https://leetcode.com/problems/shuffle-string/discuss/1174011/WEEB-DOES-PYTHON(BEATS-97.95)
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: memo,ans = {}, "" for i, j in zip(s,indices): memo[j] = i for i in range(len(memo)): ans+= memo[i] return ans
shuffle-string
WEEB DOES PYTHON(BEATS 97.95%)
Skywalker5423
1
303
shuffle string
1,528
0.857
Easy
22,651
https://leetcode.com/problems/shuffle-string/discuss/1129900/O(n)-solution-in-two-lines-with-dictionary
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: d = {idx: s[n] for n, idx in enumerate(indices)} return ''.join(d[i] for i in range(len(s)))
shuffle-string
O(n) solution in two lines with dictionary
alexanco
1
421
shuffle string
1,528
0.857
Easy
22,652
https://leetcode.com/problems/shuffle-string/discuss/1129900/O(n)-solution-in-two-lines-with-dictionary
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: result = [None] * len(s) for char, idx in zip(s, indices): result[idx] = char return ''.join(result)
shuffle-string
O(n) solution in two lines with dictionary
alexanco
1
421
shuffle string
1,528
0.857
Easy
22,653
https://leetcode.com/problems/shuffle-string/discuss/993579/Python3-O(n)-time-O(n)-space
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: length = len(indices) placeHolder = ['*'] * length for i in range(length): placeHolder[indices[i]] = s[i] return ''.join(placeHolder)
shuffle-string
Python3 O(n) time O(n) space
peterhwang
1
173
shuffle string
1,528
0.857
Easy
22,654
https://leetcode.com/problems/shuffle-string/discuss/761099/Python-3-using-Lists.-Runtime-97.33-and-Memory-100
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: len_ = len(s) res = [''] * len_ for i in range(len_): res[indices[i]] = s[i] return ''.join(res)
shuffle-string
Python 3 using Lists. Runtime 97.33% and Memory 100%
Renegade9819
1
178
shuffle string
1,528
0.857
Easy
22,655
https://leetcode.com/problems/shuffle-string/discuss/2843048/Solution-Python3
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: i=0 h='' while i in indices: h+=s[indices.index(i)] i+=1 return h
shuffle-string
Solution Python3
vovatoshev1986
0
3
shuffle string
1,528
0.857
Easy
22,656
https://leetcode.com/problems/shuffle-string/discuss/2834011/Shuffle-string-solution
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: t= [0]*len(indices) for i in range(len(indices)): t[indices[i]] = s[i] return "".join(t)
shuffle-string
Shuffle string solution
pratiklilhare
0
1
shuffle string
1,528
0.857
Easy
22,657
https://leetcode.com/problems/shuffle-string/discuss/2821354/PYTHON3-BEST
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) for index, char in enumerate(s): res[indices[index]] = char return "".join(res)
shuffle-string
PYTHON3 BEST
Gurugubelli_Anil
0
4
shuffle string
1,528
0.857
Easy
22,658
https://leetcode.com/problems/shuffle-string/discuss/2821353/PYTHON3-BEST
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) for index, char in enumerate(s): res[indices[index]] = char return "".join(res)
shuffle-string
PYTHON3 BEST
Gurugubelli_Anil
0
1
shuffle string
1,528
0.857
Easy
22,659
https://leetcode.com/problems/shuffle-string/discuss/2813244/Python-Crazy-1-liner-Time-greater-81
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: return ''.join(list(zip(*sorted(zip(indices, s))))[1])
shuffle-string
Python Crazy 1 liner Time -> 81%
RandomNPC
0
4
shuffle string
1,528
0.857
Easy
22,660
https://leetcode.com/problems/shuffle-string/discuss/2812407/Optimal-and-Clean-%3A-O(n)-Python
class Solution: # The optimal solution in python is O(n) time and space : however, can you do O(n) time and O(1) space in C++? # O(n) time : O(n) space def restoreString(self, s: str, indices: List[int]) -> str: res = [' '] * len(s) for i, idx in enumerate(indices): res[idx] = s[i] return "".join(res)
shuffle-string
Optimal and Clean : O(n) Python
topswe
0
5
shuffle string
1,528
0.857
Easy
22,661
https://leetcode.com/problems/shuffle-string/discuss/2799636/Simple-and-Easy-to-follow-Python-Solution
class Solution(object): def restoreString(self, s, indices): result=list(s) for char, idx in zip(s, indices): result[idx] = char return ''.join(result)
shuffle-string
Simple and Easy to follow Python Solution
MattCodes03
0
2
shuffle string
1,528
0.857
Easy
22,662
https://leetcode.com/problems/shuffle-string/discuss/2797447/Python-Solution-or-Beats-97-Memory
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: l=[0]*len(indices) for i in range(len(s)): l[indices[i]]=s[i] return "".join(l)
shuffle-string
Python Solution | Beats 97% Memory
sbhupender68
0
3
shuffle string
1,528
0.857
Easy
22,663
https://leetcode.com/problems/shuffle-string/discuss/2792376/Python-oror-Easy-oror-Faster-than-92.83
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: # List to store characters of input string list_str = list(s) # Loop to iterate over index numbers of indices list for i in range(len(indices)): list_str[indices[i]] = s[i] # Join characters of resultant list to return a string return ''.join(list_str)
shuffle-string
Python || Easy || Faster than 92.83%
pawangupta
0
4
shuffle string
1,528
0.857
Easy
22,664
https://leetcode.com/problems/shuffle-string/discuss/2777544/Python-Solution-oror-O(N)-Easy-to-understand
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: n = len(s) ans = [0]*n # initialize of length of string s for i in range(n): # get the value of each shuffles character in its original position ans[indices[i]] = s[i] return "".join(ans) # converting list to string
shuffle-string
Python Solution || O(N) - Easy to understand
vishal7085
0
2
shuffle string
1,528
0.857
Easy
22,665
https://leetcode.com/problems/shuffle-string/discuss/2769700/python-using-dictionary
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: new_dict = {} for i in range(len(indices)): for j in range(len(s)): if i == j: new_dict.update({indices[i]:s[j]}) sortedList = sorted(new_dict.items()) finalstring ="" for i in sortedList: finalstring = finalstring + i[1] return finalstring
shuffle-string
python using dictionary
user7798V
0
5
shuffle string
1,528
0.857
Easy
22,666
https://leetcode.com/problems/shuffle-string/discuss/2757434/Python-Solution
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: arr = [None] * len(s) for i,v in enumerate(s): arr[indices[i]] = s[i] return "".join(arr)
shuffle-string
Python Solution
Jashan6
0
3
shuffle string
1,528
0.857
Easy
22,667
https://leetcode.com/problems/shuffle-string/discuss/2671636/Python-iterative
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: ans = [""] * len(s) for i, c in enumerate(indices): ans[c] = s[i] return "".join(ans)
shuffle-string
Python - iterative
phantran197
0
3
shuffle string
1,528
0.857
Easy
22,668
https://leetcode.com/problems/shuffle-string/discuss/2668673/Simple-python-code-with-explanation
class Solution: def restoreString(self, s, indices): #create a list(res) of length of indices and fill it with zeros res = [0]*len(indices) #iterate over the elements in string(s) for i in range(len(s)): #index of s[i] is stored in indices[i] #so change the res[indices[i]] as the s[i] res[indices[i]] = s[i] #then join all the elements in res list as a string res = "".join(res) #then return that res(string) return res
shuffle-string
Simple python code with explanation
thomanani
0
8
shuffle string
1,528
0.857
Easy
22,669
https://leetcode.com/problems/shuffle-string/discuss/2666377/Python-or-Time%3A-O(n)-and-Space-O(1)
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: result = [" "] * len(indices) for i in range(len(indices)): result[indices[i]] = s[i] return "".join(result)
shuffle-string
Python | Time: O(n) and Space O(1)
user9015KF
0
3
shuffle string
1,528
0.857
Easy
22,670
https://leetcode.com/problems/shuffle-string/discuss/2507780/Easy-solution-using-index-approach-in-Python
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: n = len(s) res = [i for i in range(n)] for i in range(len(s)): val = s[i] idx = indices[i] res[idx] = val return ''.join(res)
shuffle-string
Easy solution using index approach in Python
ankurbhambri
0
61
shuffle string
1,528
0.857
Easy
22,671
https://leetcode.com/problems/shuffle-string/discuss/2459130/Python-or-zip-dict-join
class Solution: def restoreString(self, string: str, indices: List[int]) -> str: ind_string = dict(zip(indices, string)) res = [ind_string[i] for i, _ in enumerate(string)] return ''.join(res)
shuffle-string
Python | zip, dict, join
Wartem
0
72
shuffle string
1,528
0.857
Easy
22,672
https://leetcode.com/problems/shuffle-string/discuss/2426573/easiest-python-solution
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [""] * len(s) for i in range(len(s)): res[indices[i]] = s[i] return "".join(res)
shuffle-string
easiest python solution
Abdulrahman_Ahmed
0
78
shuffle string
1,528
0.857
Easy
22,673
https://leetcode.com/problems/shuffle-string/discuss/2375826/Python-Dict-Easy
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: d = {index: char for index, char in zip(indices, s)} return ''.join([d[index] for index in range(len(s))])
shuffle-string
Python Dict Easy
Arrstad
0
43
shuffle string
1,528
0.857
Easy
22,674
https://leetcode.com/problems/shuffle-string/discuss/2323094/Python-4-Lines-or-Easy-Solution
class Solution(object): def restoreString(self, s, indices): ans = list(s) for char, index in zip(s, indices): ans[index] = char return "".join(ans)
shuffle-string
Python 4 Lines | Easy Solution
ufultra
0
86
shuffle string
1,528
0.857
Easy
22,675
https://leetcode.com/problems/shuffle-string/discuss/2304937/Python3-Two-simple-solutions-O(N)-space
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: # Space O(N) res = [""] * len(s) for i in range(len(s)): res[indices[i]] = s[i] return "".join(res) # Space O(N) with cyclic sort str1 = list(s) for i in range(len(indices)): while i != indices[i]: temp1 = indices[i] indices[i], indices[temp1] = indices[temp1], indices[i] str1[i], str1[temp1] = str1[temp1], str1[i] return "".join(str1)
shuffle-string
[Python3] Two simple solutions O(N) space
Gp05
0
46
shuffle string
1,528
0.857
Easy
22,676
https://leetcode.com/problems/shuffle-string/discuss/2274496/Python-3-Runtime%3A-62ms-oror-Faster-than-86.59-of-python-3-submissions
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = "" i = 0 def driver(s: str, indices: List[int], i: int): nonlocal res if i == len(s) or len(s) == 0: return res res += s[indices.index(i)] driver(s, indices, i+1) driver(s, indices, i) return res
shuffle-string
Python 3 Runtime: 62ms || Faster than 86.59% of python 3 submissions
Gilbert770
0
40
shuffle string
1,528
0.857
Easy
22,677
https://leetcode.com/problems/shuffle-string/discuss/2239338/Python3-solution-using-hashmap
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: d={} pointer=0 res='' for i in indices: d[i] = s[pointer] pointer+=1 for key in sorted(d.keys()): res+=d[key] return (res)
shuffle-string
Python3 solution using hashmap
psnakhwa
0
42
shuffle string
1,528
0.857
Easy
22,678
https://leetcode.com/problems/shuffle-string/discuss/2147813/SHUFFLE-STRING-or-easy-and-simple-solution
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: l=[0]*len(s) for i in range(len(indices)): l[indices[i]]=s[i] return "".join(l)
shuffle-string
SHUFFLE STRING | easy and simple solution
T1n1_B0x1
0
77
shuffle string
1,528
0.857
Easy
22,679
https://leetcode.com/problems/shuffle-string/discuss/2126644/hmap-and-list-solution-O(n)
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: # using list shuffled = [""] * len(s) for char, i in zip(s, indices): shuffled[i] = char return "".join(shuffled) # using hashmap shuffled = "" hmap = {i: char for char, i in zip(s, indices)} for i in sorted(hmap): shuffled += hmap[i] return shuffled
shuffle-string
hmap & list solution O(n)
andrewnerdimo
0
48
shuffle string
1,528
0.857
Easy
22,680
https://leetcode.com/problems/shuffle-string/discuss/2023008/Python3-simple-code
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: sort = '' for i in range(len(indices)): sort += s[indices.index(i)] return sort
shuffle-string
[Python3] simple code
Shiyinq
0
103
shuffle string
1,528
0.857
Easy
22,681
https://leetcode.com/problems/shuffle-string/discuss/1979312/1528.-Shuffle-String
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: D = {} for i in range(len(s)): D[indices[i]] = s[i] sort_tuple = sorted(D.items(),key = lambda item:item[0]) return ''.join([v for k,v in sort_tuple])
shuffle-string
1528. Shuffle String
user4774i
0
28
shuffle string
1,528
0.857
Easy
22,682
https://leetcode.com/problems/shuffle-string/discuss/1915973/Python3-Two-kind-of-solutions%3A-cyclic-sort-and-with-extra-space
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) for i in range(len(s)): res[indices[i]] = s[i] return ''.join(res) class Solution: def restoreString(self, s: str, indices: List[int]) -> str: s = list(s) for i in range(len(indices)): while i != indices[i]: tmp = indices[i] indices[i] = indices[tmp] indices[tmp] = tmp s[i], s[tmp] = s[tmp], s[i] return ''.join(s)
shuffle-string
[Python3] Two kind of solutions: cyclic sort and with extra space
maosipov11
0
27
shuffle string
1,528
0.857
Easy
22,683
https://leetcode.com/problems/shuffle-string/discuss/1866701/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: slist = list(s) output = [0]*len(s) op = "" for i,n in enumerate(indices): output[n] = slist[i] for i in output: op+=i return op
shuffle-string
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
62
shuffle string
1,528
0.857
Easy
22,684
https://leetcode.com/problems/shuffle-string/discuss/1863265/Python-solution-faster-than-91
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: temp = [""] * (len(s)) for i in range(len(indices)): temp[indices[i]] = s[i] return ''.join(temp)
shuffle-string
Python solution faster than 91%
alishak1999
0
83
shuffle string
1,528
0.857
Easy
22,685
https://leetcode.com/problems/shuffle-string/discuss/1854898/Efficient-and-easy-to-understand-Python
class Solution: def restoreString(self, s: str, indices: List[int]) -> str: fs = '' num = 0 for i in range(len(indices)): ind = indices.index(num) fs += s[ind] num += 1 return fs
shuffle-string
Efficient and easy to understand Python
natscripts
0
59
shuffle string
1,528
0.857
Easy
22,686
https://leetcode.com/problems/minimum-suffix-flips/discuss/755814/Python3-1-line
class Solution: def minFlips(self, target: str) -> int: return len(list(groupby("0" + target)))-1
minimum-suffix-flips
[Python3] 1-line
ye15
16
872
minimum suffix flips
1,529
0.724
Medium
22,687
https://leetcode.com/problems/minimum-suffix-flips/discuss/755814/Python3-1-line
class Solution: def minFlips(self, target: str) -> int: ans = flip = 0 for bulb in target: if flip ^ int(bulb): flip ^= 1 ans += 1 return ans
minimum-suffix-flips
[Python3] 1-line
ye15
16
872
minimum suffix flips
1,529
0.724
Medium
22,688
https://leetcode.com/problems/minimum-suffix-flips/discuss/755814/Python3-1-line
class Solution: def minFlips(self, target: str) -> int: ans, prev = 0,"0" for c in target: if prev != c: ans += 1 prev = c return ans
minimum-suffix-flips
[Python3] 1-line
ye15
16
872
minimum suffix flips
1,529
0.724
Medium
22,689
https://leetcode.com/problems/minimum-suffix-flips/discuss/1190315/Python-O(n)-solution-with-Intuition
class Solution: def minFlips(self, target: str) -> int: ''' Intuition: the min number of flips equals the number of toggles between 0 and 1 starting with 0. prev: previous character (0 at first) ''' flips = 0 prev = '0' for num in target: if num != prev: flips += 1 prev = num return flips
minimum-suffix-flips
Python O(n) solution with Intuition
jitin11
2
168
minimum suffix flips
1,529
0.724
Medium
22,690
https://leetcode.com/problems/minimum-suffix-flips/discuss/2803422/Python3-Solution-with-using-greedy
class Solution: def minFlips(self, target: str) -> int: res = 0 prev = '0' for idx in range(len(target)): if target[idx] != prev: prev = target[idx] res += 1 return res
minimum-suffix-flips
[Python3] Solution with using greedy
maosipov11
0
4
minimum suffix flips
1,529
0.724
Medium
22,691
https://leetcode.com/problems/minimum-suffix-flips/discuss/2787130/Python3-simple-solution
class Solution: def minFlips(self, target: str) -> int: n = len(target) pre = target[n-1] ret = 0 if pre == '1': ret += 1 for i in range(n-1, -1, -1): if target[i] == '1': if pre == 0: ret += 2 pre = 1 else: # target[i] == '0' pre = 0 return ret
minimum-suffix-flips
[Python3] simple solution
tada_24
0
2
minimum suffix flips
1,529
0.724
Medium
22,692
https://leetcode.com/problems/minimum-suffix-flips/discuss/2381306/python-3-or-simple-greedy-solution-or-O(n)O(1)
class Solution: def minFlips(self, target: str) -> int: flip = False res = 0 for c in target: if (c == '1') != flip: flip = not flip res += 1 return res
minimum-suffix-flips
python 3 | simple greedy solution | O(n)/O(1)
dereky4
0
29
minimum suffix flips
1,529
0.724
Medium
22,693
https://leetcode.com/problems/minimum-suffix-flips/discuss/1795450/Python-Solution
class Solution: def minFlips(self, target: str) -> int: s = '1' ans = 0 for i in range(0,len(target)): if target[i] == s and s == '1': ans += 1 s = '0' elif target[i] == s and s == '0': ans += 1 s = '1' return ans
minimum-suffix-flips
Python Solution
MS1301
0
38
minimum suffix flips
1,529
0.724
Medium
22,694
https://leetcode.com/problems/minimum-suffix-flips/discuss/1533626/Python-or-1-Liner-or-O(n)-Time-O(1)-Space
class Solution: def minFlips(self, target: str) -> int: return int(target[0]) + sum(target[i] != target[i + 1] for i in range(len(target) - 1))
minimum-suffix-flips
Python | 1 Liner | O(n) Time O(1) Space
leeteatsleep
0
53
minimum suffix flips
1,529
0.724
Medium
22,695
https://leetcode.com/problems/minimum-suffix-flips/discuss/1533626/Python-or-1-Liner-or-O(n)-Time-O(1)-Space
class Solution: def minFlips(self, target: str) -> int: flips = int(target[0]) for i in range(len(target) - 1): flips += target[i] != target[i + 1] return flips
minimum-suffix-flips
Python | 1 Liner | O(n) Time O(1) Space
leeteatsleep
0
53
minimum suffix flips
1,529
0.724
Medium
22,696
https://leetcode.com/problems/minimum-suffix-flips/discuss/1380078/Python3-or-keep-track-of-current-state
class Solution: def minFlips(self, target: str) -> int: curr='0' #current state as string is "0000...." tt=curr.maketrans('10','01') #translation table to flip curr variable flip=0 for i in range(len(target)): if target[i]!=curr: #if target[i] is not equal to curr then flip curr curr=curr.translate(tt) flip+=1 return flip
minimum-suffix-flips
Python3 | keep track of current state
swapnilsingh421
0
33
minimum suffix flips
1,529
0.724
Medium
22,697
https://leetcode.com/problems/minimum-suffix-flips/discuss/1345922/Simple-4-line-python-solution-28ms-faster-than-100
class Solution: def minFlips(self, target: str) -> int: target = '0' + target value = 2*target.count('01') value -= 1 if target[-1] != '0' else 0 return value
minimum-suffix-flips
Simple 4 line python solution, 28ms faster than 100%
MrAlpha786
0
45
minimum suffix flips
1,529
0.724
Medium
22,698
https://leetcode.com/problems/minimum-suffix-flips/discuss/1185638/Python3-simple-solution-using-three-approaches
class Solution: def minFlips(self, target: str) -> int: s = '0'*len(target) i = 0 count = 0 while s != target and i < len(s): if s[i] != target[i] and s[i] == '0': s = s[:i] + '1' * (len(s)-i) count += 1 if s[i] != target[i] and s[i] == '1': s = s[:i] + '0' * (len(s)-i) count += 1 i += 1 return count
minimum-suffix-flips
Python3 simple solution using three approaches
EklavyaJoshi
0
42
minimum suffix flips
1,529
0.724
Medium
22,699