Server Side Programming Articles - Page 1877 of 2650

Find K-Length Substrings With No Repeated Characters in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:31:57

543 Views

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

Keys and Rooms in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:29:03

539 Views

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

Max Chunks To Make Sorted in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:26:16

458 Views

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

Partition Labels in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:23:37

413 Views

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

Daily Temperatures in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:20:55

1K+ Views

Suppose we have a list of daily temperatures T, we have to return a list such that, for each day in the input, shows how many days we would have to wait until a warmer temperature. If there is no future day for which this is possible, store 0 instead. For example, if T = [73, 74, 75, 71, 69, 72, 76, 73], output will be [1, 1, 4, 2, 1, 1, 0, 0].To solve this, we will follow these steps −ans := an array of size same as T, and fill this with 0define one stack, and insert 0 ... Read More

Subarray Product Less Than K in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:17:39

367 Views

Suppose we have given an array of positive integers nums. We have to count and print the number of (contiguous) subarrays where the product of each the elements in the subarray is less than k. So if the input is like [10,5,2,6] and k := 100, then the output will be 8. So the subarrays will be [[10], [5], [2], [6], [10, 5], [5, 2], [2, 6] and [5, 2, 6]]To solve this, we will follow these steps −temp := 1, j := 0 and ans := 0for i in range 0 to size of the arraytemp := temp * nums[i]while temp >= k and j = k && j

Minimum ASCII Delete Sum for Two Strings in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:12:07

455 Views

Suppose we have two words w1 and w2, we have to find the lowest ASCII sum of deleted characters to make w1 and w2 the same, where in each step we can delete one character in either string. So if the input is like “sea” and “eat”, then the output will be 231, because we need to delete ‘s’ from w1, this will be “ea” and delete “t” from “eat” from w2. Then they are same. Deleting ‘t’ from “eat” adds 116 to the sum, and at the end, both strings are the same and 115 + 116 = 231 ... Read More

Insert into a Binary Search Tree in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:08:57

8K+ Views

Suppose we have a binary search tree. we have to write only one method, that performs the insertion operation with a node given as a parameter. We have to keep in mind that after the operation, the tree will remain BST also. So if the tree is like −if we insert 5, then the tree will be −To solve this, we will follow these steps −This method is recursive. this is called insert(), this takes a value v.if root is null, then create a node with given value v and make that as rootif value of root > v, thenleft ... Read More

Partition to K Equal Sum Subsets in C++

Arnab Chakraborty
Updated on 29-Apr-2020 07:59:22

452 Views

Suppose we have an array of integers called nums and a positive integer k, check whether it's possible to divide this array into k non-empty subsets whose sums are all same. So if the array is like [4, 3, 2, 3, 5, 2, 1] and k = 4, then the result will be True, as the given array can be divided into four subarray like [[5], [1, 4], [2, 3], [2, 3]] with equal sums.To solve this, we will follow these steps −define two table called dp and total of size 2^n, sort the given array nums, set sum := ... Read More

Top K Frequent Words in C++

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:40

600 Views

Suppose we have a non-empty list of words; we have to find the k most frequent elements. our answer should be sorted by frequency from highest to lowest. When two words have the same frequency, then the word with the lower alphabetical order will be placed at first. So if the array is like [‘the’, ‘sky’, ‘is’, ‘blue’, ‘the’, ‘weather’, ‘is’, ‘comfortable’], so most frequent words are ["is", "the", "blue"]To solve this, we will follow these steps −Define one map called mcreate one priority queue vfor i := 0 to n, where n is the size of the word array, ... Read More

Advertisements