Suppose we have a string s. We have to find all the words vertically in the same order in which they appear in s. Here words are returned as a list of strings, we have to complete with spaces when is necessary. (Trailing spaces are not allowed). Each word would be put on only one column and that in one column there will be only one word. So if the input string is “HOW ARE YOU”, then the output will be [“HAY”, “ORO”, “WEU”]To solve this, we will follow these steps −s := make a list of strings split by ... Read More
Before Java 9, the extension and the application class loader are an instance of the java.net.URLClassLoader class. In Java 9, the classification of class loaders has changed, instead of an external class loader, we have the Platform class loader. The purpose of using Platform class loader is that classes loaded by the bootstrap class loader have all permissions by default.In the below example, we can display all modules with classloaders.Exampleimport static java.util.Objects.isNull; public class Java9ClassLoaderTest { public static void main(String args[]) { ModuleLayer layer = ModuleLayer.boot(); layer.modules().forEach(module -> { ... Read More
Suppose we have an integer n. We have to return any array that contains n unique integers, such that they add up to 0. So if input is n = 5, then one possible output will be [-7, -1, 1, 3, 4]To solve this, we will follow these steps −take an array A as final answer, and take x := 0for i in range 0 to n – 2A[i] = (i + 1)x := x + i + 1A[n – 1] = xreturn AExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){ cout
Suppose we have a string S, we have to find the number of substrings of length K where no characters are repeated. So if S = “heyfriendshowareyou” and K is 5, then output will be 15, as the strings are [heyfr, eyfri, yfrie, frien, riend, iends, endsh, ndsho, dshow, showa, howar, oware, warey, areyo, reyou]To solve this, we will follow these steps −create one empty map m, and left := 0 and right := -1 and ans := 0while right < length of string – 1if right – left + 1 = k, thenincrease ans by 1decrease m[str[left]] by 1increase ... Read More
Suppose we have N rooms and we start in room 0. In each room there exists a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. So in other words, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = number of rooms. A key rooms[i][j] = v, this opens the room with number v So if the input is [[1], [2], [3], []]. then output will be true. There are few more points that ... Read More
Suppose we have an array A. We have to replace every element by the greatest element on the right side of this element. And replace the last one by -1. So if A = [5, 17, 40, 6, 3, 8, 2], then it will be [40,40,8,8,8,2,-1]To solve this, we will follow these steps −We will read the array element from right to left.take e := -1for i := n – 1 to 0temp := ee := max between e and array[i]array[i] := tempreturn arrayExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){ cout
Suppose we gave an array arr that is a permutation of [0, 1, ..., arr.length - 1], we have to split the array into some number of "chunks" or partitions, and individually sort each partition. So after concatenating them, the result will be the sorted array. So if the array is like [1, 0, 2, 3, 4], then the output will be 4, as we can split into two partitions like [1, 0] and [2, 3, 4], but this can also be true that [1, 0], [2], [3], [4]. So this is the highest number of chunks possible, so output ... Read More
Suppose we have a list of numbers. We have to count the numbers that has even number of digit count. So if the array is like [12, 345, 2, 6, 7896], the output will be 2, as 12 and 7896 has even number of digitsTo solve this, we will follow these steps −Take the list and convert each integer into stringif the length of string is even, then increase count and finally return the count valueExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object): def findNumbers(self, nums): str_num = map(str, nums) ... Read More
Suppose we have a string S of lowercase letters is given. We will partition this string into as many parts as possible so that each letter appears in at most one part, finally return a list of integers representing the size of these parts. So if the string is like “ababcbacadefegdehijhklij”, output is [9, 7, 8], because the partitions are “ababcbaca”, “defegde”, “hijhklij”. So this is a partition so that each letter occurs in at most one part. A partition like "ababcbacadefegde", "hijhklij" is not correct, because it splits S into less parts.To solve this, we will follow these steps ... Read More
Suppose we have an array A. There are few elements. Some elements are common. We have to return an element that is appearing more than 25% spaces in the array. So if A = [1, 2, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7], Here 4 has occurred four times. This is more than 25% of 12 (size of the array)To solve this, we will follow these steps −Read elements and store their respective frequenciesIf the frequency is greater than 25% of the array size, then return the result.ExampleLet us see the following implementation to get better understanding ... Read More