├── Base 3 to Integer.py ├── Pair Sums.py ├── Pangram.py ├── Corner Diagonals.py ├── Caesar Cipher.py ├── Median Minimization.py ├── Rectangular Overlap.py ├── Minimum String.py ├── Equivalent Value and Frequency.py ├── Making Change.py ├── N Lexicographic Integers.py ├── No New Friends.py ├── Bob's Game.py ├── Largest Sublist Sum.py ├── Largest and Smallest Difference.py ├── Pythagorean Triplets.py ├── Largest Anagram Group.py ├── Shortest String.py ├── Unix Path Resolution.py ├── Longest Common Prefix.py ├── Interval Intersection.py ├── Connell Sequence.py ├── Latin Square.py ├── Repeated K-Length Substrings.py ├── Rotation Groups.py ├── Boss Fight.py ├── Sum of Two Numbers.py ├── Palindromic Anagram.py ├── Happy Numbers.py ├── String Sequence.py ├── Recursive Index.py ├── Remove Interval Overlaps.py ├── Accumulator Battery.py ├── Longest Interval.py ├── FizzBuzz.py ├── Minimum Bracket Addition.py ├── Pascal's Triangle.py ├── Package Versioning.py ├── Stacks.py ├── Run-Length Encoding.py ├── Interval Carving.py ├── One Interval.py ├── Repeated Deletion.py ├── Rotating Primes.py ├── Squeezed List.py ├── Condo Developers.py ├── Rocketship Rescue.py ├── Double, Reverse, and Swap.py ├── Vanity Phone Numbers.py ├── Maximum Removal Subsequence String.py └── README.md /Base 3 to Integer.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Traditional method/way to find the decimal from ternary. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | return sum(int(s[i]) * 3 ** (len(s) - i - 1) for i in range(len(s))) -------------------------------------------------------------------------------- /Pair Sums.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Even + Odd = Odd 4 | Using this condition, here I make combinations and generalize them to a formula. 5 | """ 6 | class Solution: 7 | def solve(self, nums): 8 | e = [i for i in nums if i % 2 == 0] 9 | return (len(nums) - len(e)) * len(e) -------------------------------------------------------------------------------- /Pangram.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | We make a set of all alphabets and simply check whether all are present in string s or not. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | ct = set(list("qwertyuiopasdfghjklzxcvbnm")) 8 | return not [i for i in ct if i not in s.lower()] -------------------------------------------------------------------------------- /Corner Diagonals.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | For even n there are 2n diagonals and for odd there are 2n-1 diagonals so accordingly values are returned. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | if n % 2 == 0: 8 | return (n ** 2) - (2 * n) 9 | return (n ** 2) - (2 * n - 1) -------------------------------------------------------------------------------- /Caesar Cipher.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all we find the ASCII value for all alphabets than add k and use (%26) to make in the range of [0,25] and then add 97 to get the ASCII value and return the string. 4 | """ 5 | class Solution: 6 | def solve(self, s, k): 7 | return "".join([chr((ord(i) + k - 97) % 26 + 97) for i in s]) -------------------------------------------------------------------------------- /Median Minimization.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all we sort it and then we just check the (nums//2-1)th and (nums//2)th value and return it's value of difference. This is the simplest approach we can use. 4 | """ 5 | class Solution: 6 | def solve(self, nums): 7 | nums.sort() 8 | return nums[len(nums) // 2] - nums[(len(nums) // 2 - 1)] -------------------------------------------------------------------------------- /Rectangular Overlap.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Simple condition to check whether the rectangles overlap or not. 4 | """ 5 | class Solution: 6 | def solve(self, rect0, rect1): 7 | if (rect0[2] > rect1[0] and rect0[3] > rect1[1]) and ( 8 | rect1[2] > rect0[0] and rect1[3] > rect0[1] 9 | ): 10 | return True 11 | return False -------------------------------------------------------------------------------- /Minimum String.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Return the sum of all positive differences for count of alphabets from String t in String s. 4 | """ 5 | class Solution: 6 | def solve(self, s, t): 7 | c1 = Counter(s) 8 | c2 = Counter(t) 9 | ct = 0 10 | for i, val in c2.items(): 11 | ct += max(val - c1.get(i, 0), 0) 12 | return ct -------------------------------------------------------------------------------- /Equivalent Value and Frequency.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we just compare the number with it's frequency using the Counter() function and return whether True or False. 4 | """ 5 | class Solution: 6 | def solve(self, nums): 7 | ct = Counter(nums) 8 | for i, j in ct.items(): 9 | if i == j: 10 | return True 11 | return False -------------------------------------------------------------------------------- /Making Change.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | One by one we iterate and get the count. 4 | eg: n=49 5 | i=25 : ct=1,n=24 6 | i=10 : ct=3,n=4 7 | i=5 : ct=3,n=4 8 | i=1 : ct=7,n=0 9 | """ 10 | class Solution: 11 | def solve(self, n): 12 | ct = 0 13 | for i in [25, 10, 5, 1]: 14 | if n >= i: 15 | ct += n // i 16 | n %= i 17 | return ct -------------------------------------------------------------------------------- /N Lexicographic Integers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all I created a list of string of numbers from 1 to n and then sorted it will give me in lexicographic manner and then return the list as integers. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | ls = [str(i) for i in range(1, n + 1)] 8 | ls.sort() 9 | ls = [int(i) for i in ls] 10 | return ls -------------------------------------------------------------------------------- /No New Friends.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we just add the elements in a set and check whether the length is the same as n or not. If the element is not in the set then it'll have now friends. 4 | """ 5 | class Solution: 6 | def solve(self, n, friends): 7 | s = set() 8 | for i, j in friends: 9 | s.add(i) 10 | s.add(j) 11 | return len(s) == n -------------------------------------------------------------------------------- /Bob's Game.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | If count of odd values are found even then divide by 2 gives the answer and if odd count found then there is no way to solve all so -1. 4 | """ 5 | class Solution: 6 | def solve(self, nums): 7 | odd = 0 8 | for i in nums: 9 | if i % 2 == 1: 10 | odd += 1 11 | if odd % 2 == 1: 12 | return -1 13 | return odd // 2 -------------------------------------------------------------------------------- /Largest Sublist Sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | One by one we add all the numbers till sum always remains positive and then add them to ma and return them. 4 | """ 5 | class Solution: 6 | def solve(self, nums): 7 | if len(nums) == 1: 8 | return nums[0] 9 | ma = 0 10 | s = 0 11 | for i in nums: 12 | s = max(s + i, 0) 13 | ma = max(ma, s) 14 | return ma -------------------------------------------------------------------------------- /Largest and Smallest Difference.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we calculate all the differences for the i-th and (i+k-1)th values after sorting nums and return the minimum value from the list. 4 | """ 5 | class Solution: 6 | def solve(self, nums, k): 7 | nums.sort() 8 | ls = [] 9 | for i in range(len(nums) - k + 1): 10 | ls.append(nums[i + k - 1] - nums[i]) 11 | return min(ls) -------------------------------------------------------------------------------- /Pythagorean Triplets.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all we add all the squares of a number in the set and then check whether (i x i) + (j x j) exists in the set or not. 4 | """ 5 | class Solution: 6 | def solve(self, nums): 7 | st = set(i * i for i in nums) 8 | for i in nums: 9 | for j in nums: 10 | if (i * i + j * j) in st: 11 | return True 12 | return False -------------------------------------------------------------------------------- /Largest Anagram Group.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we used dictionary to store the Count of the anagrams. We check the count of each word by sorting it and increasing it's count and returning it's maximum count. 4 | """ 5 | class Solution: 6 | def solve(self, words): 7 | ct = dict() 8 | for i in words: 9 | temp = "".join(sorted(i)) 10 | ct[temp] = ct.get(temp, 0) + 1 11 | return max(ct.values()) -------------------------------------------------------------------------------- /Shortest String.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Traverse through all values and check whether same then append if different then pop values and return the length. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | ls = [] 8 | for i in s: 9 | if len(ls) == 0: 10 | ls.append(i) 11 | elif ls[-1] == i: 12 | ls.append(i) 13 | else: 14 | ls.pop() 15 | return len(ls) -------------------------------------------------------------------------------- /Unix Path Resolution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | If ".." then pop() values from the list else append values till the end of traversal. 4 | """ 5 | class Solution: 6 | def solve(self, path): 7 | ls = [] 8 | for i in path: 9 | if i == "..": 10 | if len(ls) != 0: 11 | ls.pop() 12 | elif i == ".": 13 | pass 14 | else: 15 | ls.append(i) 16 | return ls -------------------------------------------------------------------------------- /Longest Common Prefix.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | We take the word with minimum length and then check until the prefixes don't match in all the words. 4 | """ 5 | class Solution: 6 | def solve(self, words): 7 | st = "" 8 | temp = "" 9 | mi = min(words) 10 | for i in mi: 11 | temp += i 12 | for j in words: 13 | if temp not in j: 14 | return st 15 | st = temp 16 | return st -------------------------------------------------------------------------------- /Interval Intersection.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, I go to every intersection's first and last limit and check the necessary conditions like < and > respectively and return the value. 4 | """ 5 | class Solution: 6 | def solve(self, intervals): 7 | l = intervals[0][0] 8 | h = intervals[0][1] 9 | for i in intervals: 10 | if l < i[0]: 11 | l = i[0] 12 | if h > i[1]: 13 | h = i[1] 14 | return [l, h] -------------------------------------------------------------------------------- /Connell Sequence.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | To change from odd to even or from even to odd, we add 1 to the sequence and to iterate through either odd or even we add 2 for the following range of count. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | ls = [1] 8 | ct = 1 9 | while len(ls) <= n: 10 | ls.append(ls[-1] + 1) 11 | for i in range(ct): 12 | ls.append(ls[-1] + 2) 13 | ct += 1 14 | return ls[n] -------------------------------------------------------------------------------- /Latin Square.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First we check the Count for each row and then each column and if it doesn't match then False else True. 4 | """ 5 | class Solution: 6 | def solve(self, matrix): 7 | count = Counter(matrix[0]) 8 | 9 | for i in matrix: 10 | if count != Counter(i): 11 | return False 12 | 13 | for i in zip(*matrix): 14 | if count != Counter(i): 15 | return False 16 | 17 | return True -------------------------------------------------------------------------------- /Repeated K-Length Substrings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we use dictionary to make the work easier. A Counter() can also be used to make it easy. Just count the slices of k-length slices in the string s and return the sum of slices appearing more than once. 4 | """ 5 | class Solution: 6 | def solve(self, s, k): 7 | d = {} 8 | for i in range(len(s) - k + 1): 9 | d[s[i : i + k]] = d.get(s[i : i + k], 0) + 1 10 | return sum(1 for i, val in d.items() if val > 1) -------------------------------------------------------------------------------- /Rotation Groups.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, I make every rotation of the string and add them to set. So whenever another rotation occurs then the set will identify and count will not be incremented. 4 | """ 5 | class Solution: 6 | def solve(self, words): 7 | s = set() 8 | ct = 0 9 | 10 | for i in words: 11 | if i not in s: 12 | ct += 1 13 | for j in range(len(i)): 14 | s.add(i[j:] + i[:j]) 15 | return ct -------------------------------------------------------------------------------- /Boss Fight.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | We keep the indexes to be removed by appending them in a list and then remove them using pop in the end. 4 | """ 5 | class Solution: 6 | def solve(self, fighters, bosses): 7 | n = fighters.count(1) 8 | temp = [] 9 | for i in range(len(bosses)): 10 | if bosses[i].count(1) < n: 11 | temp.append(i) 12 | a = 0 13 | for i in temp: 14 | bosses.pop(i - a) 15 | a += 1 16 | return bosses -------------------------------------------------------------------------------- /Sum of Two Numbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we check values in the set and if value found equal to (k-i) then return True else False. 4 | """ 5 | class Solution: 6 | def solve(self, nums, k): 7 | if len(nums) == 1: 8 | return False 9 | s = set(nums) 10 | for i in nums: 11 | if k - i in s: 12 | if k - i == i: 13 | if nums.count(i) <= 1: 14 | continue 15 | return True 16 | return False -------------------------------------------------------------------------------- /Palindromic Anagram.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here we use the Counter() to check whether string is palindrome or not. If count of the alphabet is odd viz. 'e' in "racecar" then there should be only one alphabet that can be odd. If all counts are even then it can be used to from a palindrome. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | ct = 0 8 | count = Counter(s) 9 | for i, val in count.items(): 10 | if val % 2 != 0: 11 | ct += 1 12 | return ct == 0 or ct == 1 -------------------------------------------------------------------------------- /Happy Numbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | One by one we add n in set if not identified. After that we add (all digits of n ** 2) and recheck until loop is formed or n==1. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | s = set() 8 | while n != 1: 9 | if n in s: 10 | return False 11 | else: 12 | s.add(n) 13 | val = n 14 | n = 0 15 | while val > 0: 16 | n += (val % 10) ** 2 17 | val //= 10 18 | return True -------------------------------------------------------------------------------- /String Sequence.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition: 3 | Here one by one as asked in the problem, we append value in the list and return the last element of the list.. 4 | """ 5 | class Solution: 6 | def solve(self, s0, s1, n): 7 | if n == 0: 8 | return s0 9 | if n == 1: 10 | return s1 11 | ls = [s0, s1] 12 | for i in range(2, n + 1): 13 | if i % 2 == 0: 14 | ls.append(ls[i - 1] + ls[i - 2]) 15 | else: 16 | ls.append(ls[i - 2] + ls[i - 1]) 17 | return ls[-1] -------------------------------------------------------------------------------- /Recursive Index.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | One by one we iterate and use k=A[k] and check the conditions whether a loop is formed or return the total jumps we made. 4 | """ 5 | class Solution: 6 | def solve(self, A, k): 7 | if A == []: 8 | return 0 9 | s = set() 10 | while True: 11 | if k >= len(A): 12 | break 13 | k = A[k] 14 | if k >= len(A): 15 | break 16 | if k in s: 17 | return -1 18 | s.add(k) 19 | return len(s) + 1 -------------------------------------------------------------------------------- /Remove Interval Overlaps.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all we sort according to the end time of intervals and then one by one check whether overlapping is happening and accordingly return the count. 4 | """ 5 | class Solution: 6 | def solve(self, intervals): 7 | ct = 0 8 | intervals = sorted(intervals, key=lambda x: x[1]) 9 | e = intervals[0][1] 10 | for i in range(1, len(intervals)): 11 | if e <= intervals[i][0]: 12 | e = intervals[i][1] 13 | ct += 1 14 | return len(intervals) - ct - 1 -------------------------------------------------------------------------------- /Accumulator Battery.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | If p>20 then, 4 | Rate of degradation r=t/(100-p) 5 | Last long r till p>20 then 2*r for [0,20] then add both. 6 | 7 | if p<=20 then: 8 | Rate of degradation r=t/(80+(2*(20-p))) 9 | Because the battery drains 2 times slower so you've to add this in rate of degradation. 10 | So last long till 2*r. 11 | """ 12 | class Solution: 13 | def solve(self, t, p): 14 | if p > 20: 15 | return (t / (100 - p)) * (p - 20) + (2 * (t / (100 - p))) * 20 16 | else: 17 | return (t / (80 + 2 * (20 - p))) * p * 2 -------------------------------------------------------------------------------- /Longest Interval.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, first of all we merge all the lists that have common in a union and then we check the maximum range for the intervals from the new list. 4 | """ 5 | class Solution: 6 | def solve(self, intervals): 7 | intervals.sort() 8 | ls = [intervals[0]] 9 | for i in range(1, len(intervals)): 10 | if ls[-1][1] >= intervals[i][0]: 11 | ls[-1][1] = max(ls[-1][1], intervals[i][1]) 12 | else: 13 | ls.append(intervals[i]) 14 | return max(i[1] - i[0] + 1 for i in ls) -------------------------------------------------------------------------------- /FizzBuzz.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | As asked in the problem we first check for n % (15,3,5) in the order for all i till n and return the list. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | ls = [] 8 | st = "" 9 | for i in range(1, n + 1): 10 | if i % 3 == 0 and i % 5 == 0: 11 | st = "FizzBuzz" 12 | elif i % 3 == 0: 13 | st = "Fizz" 14 | elif i % 5 == 0: 15 | st = "Buzz" 16 | else: 17 | st = str(i) 18 | ls.append(st) 19 | return ls -------------------------------------------------------------------------------- /Minimum Bracket Addition.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here we check the open and closed brackets. If open occurs then increment c1 then if closing bracket occurs then we check whether there is any open bracket, if Yes then we decrement c1 else we increment c2. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | c1 = 0 8 | c2 = 0 9 | for i in s: 10 | if i == "(": 11 | c1 += 1 12 | else: 13 | if c1 > 0: 14 | c1 -= 1 15 | else: 16 | c2 += 1 17 | return c1 + c2 -------------------------------------------------------------------------------- /Pascal's Triangle.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Append 1 in back and front of every list and add all the consecutive numbers from the previous list and return as asked. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | if n == 0: 8 | return [1] 9 | if n == 1: 10 | return [1, 1] 11 | ls = [1, 1] 12 | temp = [1, 1] 13 | for i in range(2, n + 1): 14 | ls = temp 15 | temp = [1] 16 | for i in range(len(ls) - 1): 17 | temp.append(ls[i] + ls[i + 1]) 18 | temp.append(1) 19 | return temp -------------------------------------------------------------------------------- /Package Versioning.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we use a flag to check whether any other bigger value has occurred while traversing or not. If yes then True and if not then return False. 4 | """ 5 | 6 | class Solution: 7 | def solve(self, older, newer): 8 | if older == newer: 9 | return False 10 | lo = older.split(".") 11 | ln = newer.split(".") 12 | f = False 13 | for i, j in zip(lo, ln): 14 | if int(i) < int(j): 15 | f = True 16 | if int(i) > int(j) and f == False: 17 | return False 18 | return True -------------------------------------------------------------------------------- /Stacks.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all we add all the possible sum of the lists in a new list and then return the maximum common sum of elements found in all the stacks using functools. 4 | """ 5 | class Solution: 6 | def solve(self, stacks): 7 | if len(stacks) == 0: 8 | return 0 9 | ls = [] 10 | temp = [] 11 | for i in stacks: 12 | temp = [0] 13 | val = 0 14 | for j in i: 15 | val += j 16 | temp.append(val) 17 | ls.append(temp) 18 | ans = list(reduce(lambda i, j: i & j, (set(x) for x in ls))) 19 | return max(ans) -------------------------------------------------------------------------------- /Run-Length Encoding.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | One by one we traverse through values and count the appearance of the alphabet and accordingly add it to a string and return it. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | temp = "" 8 | st = "" 9 | ct = 0 10 | for i in s: 11 | if temp == "": 12 | temp = i 13 | ct = 1 14 | continue 15 | if temp == i: 16 | ct += 1 17 | else: 18 | st += str(ct) + temp 19 | ct = 1 20 | temp = i 21 | st += str(ct) + temp 22 | return st -------------------------------------------------------------------------------- /Interval Carving.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | If any of the [start,end] in cut lies in the list of intervals then we carve and append the new interval in a list and return it. 4 | """ 5 | class Solution: 6 | def solve(self, intervals, cut): 7 | ls = [] 8 | for i in intervals: 9 | if i[0] < cut[0] < i[1]: 10 | i[1] = cut[0] 11 | ls.append(i) 12 | elif i[0] < cut[1] < i[1]: 13 | i[0] = cut[1] 14 | ls.append(i) 15 | elif i[0] >= cut[0] and i[1] <= cut[1]: 16 | pass 17 | else: 18 | ls.append(i) 19 | return ls -------------------------------------------------------------------------------- /One Interval.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First of all we merge the intervals which overlap with each other. Then return the difference of (start of the last element) and (end of the first element). 4 | """ 5 | class Solution: 6 | def solve(self, intervals): 7 | if len(intervals) == 1: 8 | return 0 9 | intervals.sort() 10 | ls = [intervals[0]] 11 | for i in range(1, len(intervals)): 12 | if ls[-1][1] >= intervals[i][0]: 13 | ls[-1][1] = max(ls[-1][1], intervals[i][1]) 14 | else: 15 | ls.append(intervals[i]) 16 | if len(ls) == 1: 17 | return 0 18 | return ls[-1][0] - ls[0][1] -------------------------------------------------------------------------------- /Repeated Deletion.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, we use a stack to solve the problem. If new character occurs we check whether it is on our top of the stack or not, if yes then we pop it. Else if previous character is the same as new character then no need to pop as it is already popped and if neither occurs then append the new(distinct) character. 4 | """ 5 | class Solution: 6 | def solve(self, s): 7 | ls = [""] 8 | prev = "" 9 | for i in s: 10 | if i == ls[-1]: 11 | ls.pop() 12 | elif prev == i: 13 | pass 14 | else: 15 | ls.append(i) 16 | prev = i 17 | return "".join(ls) -------------------------------------------------------------------------------- /Rotating Primes.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | All rotations formed of the given number. Then check whether prime or not with O(sqrt(n)) time complexity and return accordingly. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | def check(val): 8 | i = 2 9 | while i * i <= val: 10 | if val % i == 0: 11 | return False 12 | i += 1 13 | return True 14 | 15 | ls = [] 16 | s = str(n) 17 | for i in range(len(s)): 18 | ls.append(s[i:] + s[:i]) 19 | ls = [int(i) for i in ls] 20 | 21 | for i in ls: 22 | if not check(i): 23 | return False 24 | return True -------------------------------------------------------------------------------- /Squeezed List.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | We add and pop values as given in the explanation until len(nums)!=1 and then return the new squeezed list formed. 4 | """ 5 | class Solution: 6 | def solve(self, nums): 7 | temp = nums.copy() 8 | ls = [temp] 9 | while len(nums) != 1: 10 | if len(nums) > 2: 11 | nums[1] += nums[0] 12 | nums.pop(0) 13 | nums[-2] += nums[-1] 14 | nums.pop() 15 | temp = list(nums) 16 | ls.append(temp) 17 | continue 18 | nums[1] += nums[0] 19 | nums.pop(0) 20 | temp = list(nums) 21 | ls.append(temp) 22 | return ls -------------------------------------------------------------------------------- /Condo Developers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | First we find maximum in all rows and columns. Then one by one we traverse and check whether the value is equal to it's maximum value in row/column or not. If not then assign the maximum value of that row else the maximum value of the column if it goes far the maximum value of it's row. 4 | """ 5 | class Solution: 6 | def solve(self, matrix): 7 | r = [max(i) for i in matrix] 8 | c = [max(i) for i in zip(*matrix)] 9 | for i in range(len(matrix)): 10 | for j in range(len(matrix[i])): 11 | if r[i] < c[j]: 12 | matrix[i][j] = r[i] 13 | else: 14 | matrix[i][j] = c[j] 15 | return matrix -------------------------------------------------------------------------------- /Rocketship Rescue.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here we use two pointers where i starts from front and j starts from the end after sorting the weights. If the sum of values at the pointers is <=limit then we can send the rocket and if the limit exceeds then we have only one option left that is to send weights[j] single in the rocket and repeat the same procedure. 4 | """ 5 | class Solution: 6 | def solve(self, weights, limit): 7 | weights.sort() 8 | i = 0 9 | j = len(weights) - 1 10 | ct = 0 11 | while i <= j: 12 | if weights[i] + weights[j] <= limit: 13 | ct += 1 14 | i += 1 15 | j -= 1 16 | else: 17 | ct += 1 18 | j -= 1 19 | return ct -------------------------------------------------------------------------------- /Double, Reverse, and Swap.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | As asked in the problem we set different functions on different count values and return the string made at the given value. 4 | """ 5 | class Solution: 6 | def solve(self, n): 7 | s = "xxy" 8 | ct = 0 9 | for i in range(1, n + 1): 10 | if ct == 0: 11 | s = s * 2 12 | elif ct == 1: 13 | s = s[::-1] 14 | elif ct == 2: 15 | temp = "" 16 | for j in s: 17 | if j == "x": 18 | temp += "y" 19 | else: 20 | temp += "x" 21 | s = temp 22 | if ct == 2: 23 | ct = 0 24 | else: 25 | ct += 1 26 | return s -------------------------------------------------------------------------------- /Vanity Phone Numbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here we go through the traditional iterative method of one by one traversing through the string and adding the elements of the dictionary likewise in the new list. 4 | """ 5 | class Solution: 6 | def solve(self, digits): 7 | d = { 8 | "2": "abc", 9 | "3": "def", 10 | "4": "ghi", 11 | "5": "jkl", 12 | "6": "mno", 13 | "7": "pqrs", 14 | "8": "tuv", 15 | "9": "wxyz", 16 | } 17 | ls = list(d.get(digits[0])) 18 | temp = [] 19 | for i in range(1, len(digits)): 20 | temp = sorted(ls * len(d.get(digits[i]))) 21 | c2 = 0 22 | while c2 != len(temp): 23 | for j in range(len(d.get(digits[i]))): 24 | c1 = 0 25 | while c1 != len(d.get(digits[i])): 26 | if c2 == len(temp): 27 | break 28 | temp[c2] += d.get(digits[i])[c1] 29 | c1 += 1 30 | c2 += 1 31 | ls = temp 32 | return ls -------------------------------------------------------------------------------- /Maximum Removal Subsequence String.py: -------------------------------------------------------------------------------- 1 | """ 2 | Intuition 3 | Here, there are 3 cases and we find the maximum output from the cases and return that. 4 | 5 | First is the length of the leftover string s after string t has occurred from the front. 6 | Then the length of the string s after string t has occurred from the end. 7 | At last, the in-between length between two consecutive alphabets where one index occurs from left and another index occurs from right. 8 | Maximum of any of the three cases is returned. 9 | """ 10 | class Solution: 11 | def solve(self, s, t): 12 | l = [] 13 | r = [] 14 | c1 = -1 15 | c2 = -1 16 | c3 = -1 17 | j = 0 18 | for i in range(len(s)): 19 | if s[i] == t[j]: 20 | l.append(i) 21 | j += 1 22 | if j == len(t): 23 | c1 = len(s) - i - 1 24 | break 25 | j = len(t) - 1 26 | for i in range(len(s) - 1, -1, -1): 27 | if s[i] == t[j]: 28 | r.insert(0, i) 29 | j -= 1 30 | if j == -1: 31 | c2 = i 32 | break 33 | for i in range(len(t) - 1): 34 | c3 = max(c3, r[i + 1] - l[i] - 1) 35 | return max(c1, c2, c3) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Binary Search Solutions 2 | 3 | Here are some of my solutions from the website https://binarysearch.com/ developed in Python Programming language. 4 | 5 | - Profile Name: lasa73 6 | - Profile Link: https://binarysearch.com/@/lasa73 7 | 8 | ## Solutions added for problems: 9 | 10 | 1. Accumulator Battery 11 | 2. Base 3 to Integer 12 | 3. Bob's Game 13 | 4. Boss Fight 14 | 5. Caesar Cipher 15 | 6. Condo Developers 16 | 7. Connell Sequence 17 | 8. Corner Diagonals 18 | 9. Double, Reverse, and Swap 19 | 10. Equivalent Value and Frequency 20 | 11. FizzBuzz 21 | 12. Happy Numbers 22 | 13. Interval Carving 23 | 14. Interval Intersection 24 | 15. Largest Anagram Group 25 | 16. Largest and Smallest Difference 26 | 17. Largest Sublist Sum 27 | 18. Latin Square 28 | 19. Longest Common Prefix 29 | 20. Longest Interval 30 | 21. Making Change 31 | 22. Maximum Removal Subsequence String 32 | 23. Median Minimization 33 | 24. Minimum Bracket Addition 34 | 25. Minimum String 35 | 26. N Lexicographic Integers 36 | 27. No New Friends 37 | 28. One Interval 38 | 29. Package Versioning 39 | 30. Pair Sums 40 | 31. Palindromic Anagram 41 | 32. Pangram 42 | 33. Pascal's Triangle 43 | 34. Pythagorean Triplets 44 | 36. Rectangular Overlap 45 | 37. Recursive Index 46 | 38. Remove Interval Overlaps 47 | 39. Repeated Deletion 48 | 40. Repeated K-Length Substrings 49 | 41. Rocketship Rescue 50 | 42. Rotating Primes 51 | 43. Rotation Groups 52 | 44. Run-Length Encoding 53 | 45. Shortest String 54 | 46. Squeezed List 55 | 47. Stacks 56 | 48. String Sequence 57 | 49. Sum of Two Numbers 58 | 50. Unix Path Resolution 59 | 51. Vanity Phone Numbers 60 | 61 | Give it a :star: if you like it. --------------------------------------------------------------------------------