We are given a string str[] as input. The goal is to count the number of anagram substrings present in str[]. Two strings are anagrams of each other if they contain the same number of characters and all characters occur in both. The order of characters can be different.“abc” is an anagram of “cba”, “bca” etc.Let us understand with examples.Input − str[] = “abccb”Output − Count of total anagram substrings are − 4Explanation − Anagrams are − (b, b), (c, c), (bc, cb), (bcc, ccb)Input − str = “aaa”Output − Count of total anagram substrings are − 4Explanation − Anagrams ... Read More
We are given a target array arr[] containing positive integers. The goal is to construct the target array arr[] using an initial array with all 0s. The operations that can be applied on a given empty array with all 0s will suffix increment/decrement operations.If we choose any index say i, then in case of suffix increment operation we will add 1 to all elements from index i till last index.In case of suffix decrement operation we will subtract 1 from all elements from index i till last index.Let us understand with examplesInput − arr[]= { 1, 2, 3 }Output − ... Read More
Suppose we have a list of requests where each list contains elements like [uid, time_sec] (uid is the user id and time_sec is the timestamp). This indicates the user whose id is uid has requested to a website at timestamp time_sec. We also have two values u and g where u denotes maximum number of requests that are allowed in any < 60 second frame for a given uid and g is the maximum number of requests that are allowed in any < 60 second frame globally. Now if we want to process each request one by one and rate ... Read More
Suppose we have a 2d matrix. We have to check whether we can start from some cell then move adjacent cells (up, down, left, right) of the same value, and come back to the same starting point. We cannot revisit a cell that we have visited last.So, if the input is like222121212221then the output will be True, as we can follow 2s to form a cycle.To solve this, we will follow these steps −R := row count of matrixC := column count of matrixvis := make a matrix of size R x C and fill with FalseDefine a function dfs() ... Read More
Suppose we have a binary matrix and another value k. You want to split the matrix into k pieces such that each piece contains at least one 1 in it. But there are some rules for cutting, we have to follow in order: 1. Select a direction: vertical or horizontal 2. Select an index in the matrix to cut into two sections. 3. If we cut vertically, we can no longer cut the left part but can only continue cutting the right part. 4. If we cut horizontally, we can no longer cut the top part and can only continue ... Read More
Suppose we have a string s, we have to check whether we can make this string a palindrome after deleting at most k characters or not.So, if the input is like s = "lieuvrel", k = 4, then the output will be True, we can delete three characters to get palindrome "level".To solve this, we will follow these steps −Define a function lcs() . This will take a, bm := size of a, n := size of btable := matrix of size (m + 1) x (n + 1) and fill with 0for i in range 1 to m, dofor ... Read More
Suppose we have a list of numbers called nums, we have to rearrange its order to form the largest possible number and return that as a string.So, if the input is like nums = [20, 8, 85, 316], then the output will be "88531620".To solve this, we will follow these steps −Define an array tempfor each item i in nums:insert i into temp as stringsort the array temp based on lexicographic sequence (check for two strings a, b when a concatenate b is larger than b concatenate a or not)for each string s in temp:res := res concatenate sreturn resLet ... Read More
Consider a country that is represented as a tree with N nodes and N-1 edges. Now each node represents a town, and each edge represents a road. We have two lists of numbers source and dest of size N-1. According to them the i-th road connects source[i] to dest[i]. And the roads are bidirectional. We also have another list of numbers called population of size N, where population[i] represents the population of the i-th town. We are trying to upgrade some number of towns into cities. But no two cities should be adjacent to each other and every node adjacent ... Read More
Suppose we have a list of numbers called nums and another value k, we have to find the number of subsets in the list that sum up to k. If the answer is very large then mod this with 10^9 + 7So, if the input is like nums = [2, 3, 4, 5, 7] k = 7, then the output will be 3, as We can choose the subsets [2,5],[3,4] and [7].To solve this, we will follow these steps −dp := a list of size (k + 1) and fill with 0dp[0] := 1m := 10^9 + 7for i in range 0 to size of nums - 1, dofor j in range k down to 0, decrease by 1, doif nums[i]
Suppose we have a binary matrix. Now consider an operation where we take one cell and flip it and all its neighboring cells (up, down, left, right). We have to find the minimum number of operations required such that matrix contains only 0s. If there is no solution, then return -1.So, if the input is like0010then the output will be 3.To solve this, we will follow these steps −Define an array dir of size: 4 x 2 := {{1, 0}, {0, 1}, { - 1, 0}, {0, - 1}}const int inf = 10^6Define a function getPos(), this will take i, ... Read More
 
 Data Structure
 Data Structure Networking
 Networking RDBMS
 RDBMS Operating System
 Operating System Java
 Java MS Excel
 MS Excel iOS
 iOS HTML
 HTML CSS
 CSS Android
 Android Python
 Python C Programming
 C Programming C++
 C++ C#
 C# MongoDB
 MongoDB MySQL
 MySQL Javascript
 Javascript PHP
 PHP 
		