Suppose there are words given. These are first and second, consider occurrences in some text of the form "first second third", here second comes immediately after the first, and third comes immediately after the second.For each such cases, add "third" into the answer, and show the answer. So if the text is like “lina is a good girl she is a good singer”, first = “a”, second = “good”, the answer will be [girl, singer]To solve this, we will follow these steps −text := split the string by spacesres is an empty listfor i := 0 to size of text ... Read More
Suppose there are two strings A and B. We can say that A is divisible by B, when A is created by concatenating B one or more times. So if A = “abcabc”, and B = “abc”, then A is divisible by B. In this section, we will see what is the greatest common divisor of a String. So return the largest string that divides both of the strings. So if two strings are “ABABAB”, and “ABAB”, then GCD will be “AB”To solve this, we will follow these steps −temp := shorter string between A and Bm := length of ... Read More
Suppose a set of students have to be arranged in non-decreasing order of their heights for a photograph. If we have an array of students, we have to return the minimum number of students that are not present in correct position. So if the array is like [1, 1, 4, 2, 1, 3], then output will be 3. So students with height 4, 3 and the last 1 are not standing in the correct position.To solve this, we will follow these steps −answer := 0let x := Array in sorted formley y := Arrayfor i := 0 to size of ... Read More
Suppose we have a string S of lowercase letters; a duplicate removal operation will be performed. This will be done by choosing two adjacent and equal letters, and removing them.We will repeatedly remove duplicates from S until no duplicates are remaining.Return the string after all such duplicate removals have been completed. It is guaranteed that the answer is unique.Suppose the string is “abbacaca”, then answer will be “caca”. At first delete duplicates bb, then string is “aacaca”, then remove aa, then string is “caca”, then no such duplicates are there.To solve this, we will follow these steps −Define an array ... Read More
Suppose we have some rocks, each rock has a positive integer weight. In each turn, we will take two heaviest rocks and smash them together. consider the stones have weights x and y and x 1: stones.sort() s1,s2=stones[-1],stones[-2] if s1==s2: stones.pop(-1) stones.pop(-1) else: s1 = abs(s1-s2) stones.pop(-1) stones[-1] = s1 if len(stones): return stones[-1] return 0 ob1 = Solution() print(ob1.lastStoneWeight([2,7,4,1,6,1]))Input[2,7,4,1,6,1]Output1
Suppose there are 2N persons. A company wants to organize one interview. The cost for flying the i-th person to city A is costs[i][0], and the cost for flying the i-th person to city B is costs[i][1]. We have to find the minimum cost to fly every person to a city, such that N people arrive in every city. So if the given list is [[10, 20], [30, 200], [400, 50], [30, 20]] The output will be 110. So we will send the person P1 to city A with cost 10, Second person to city A with cost 30, third ... Read More
Suppose we have an array A of integers, our output will be true if and only if we can partition the array into three non-empty parts whose sum is equal.Formally, we can partition the array if we can find the indexes i+1 < j with (A[0] + A[1] + ... + A[i] is same as A[i+1] + A[i+2] + ... + A[j-1] and A[j] + A[j-1] + ... + A[A.length - 1])So if the input is [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1], then the output will be true. Three arrays will be [0, 2, 1], ... Read More
Suppose we have a list of songs, the i-th song has a duration of time[i] seconds. We have to find the number of pairs of songs for which their total time in seconds is divisible by 60.So if the time array is like [30, 20, 150, 100, 40], then the answer will be 3. Three pairs will be (3, 150), (20, 100), (20, 40) for all cases the total duration is divisible by 60.To solve this, we will follow these steps −Take a map rem to store remainders. Set ans := 0for all elements i in time −if i is ... Read More
Suppose we have a number in decimal number system. We have to get the complement of the number in binary form, then again change it to decimal and return the result. So if the number is 20, then the binary form will be 10100, the complement will be 01011, this is 11 in decimalTo solve this, we will follow these steps −s := binary string of the number nsum := 0 and num := 1for each element i in s in reverse directionif i = ‘b’, then return sumotherwise when i = ‘0’, then sum := sum + numnum := ... Read More
Suppose we have a number in array form. So if the number is say 534, then it is stored like [5, 3, 4]. We have to add another value k with the array form of the number. So the final number will be another array of digits.To solve this, we will follow these steps −Take each number and make them string, then concatenate the stringconvert the string into integer, then add the numberThen convert it into string again, and make an array by taking each digit form the string.ExampleLet us see the following implementation to get better understanding − Live Democlass ... Read More