Suppose there is a puzzle string, a word is valid if both the following conditions valid −word contains the first letter of puzzle.For each letter in word, that letter is in puzzle.Suppose if we consider an example that, if the puzzle is like "abcdefg", then valid words are "face", "cabbage" etc; but some invalid words are "beefed" as there is no "a" and "based" as there is "s" which is not present in the puzzle.We have to find the list of answers, where answer[i] is the number of words in the given word list words that are valid with respect ... Read More
Suppose there is a binary tree. We will run a preorder depth first search on the root of a binary tree.At each node in this traversal, the output will be D number of dashes (Here D is the depth of this node), after that we display the value of this node. As we know if the depth of a node is D, the depth of its immediate child is D+1 and the depth of the root node is 0.Another thing we have to keep in mind that if a node has only one child, that child is guaranteed to be ... Read More
Suppose we have one 2-dimensional grid, there are 4 types of squares −In a square 1 is for the starting point. There will be exactly one starting square.In a square 2 is for the ending point. There will be exactly one ending square.In a square 0 is for the empty squares and we can walk over.In a square -1 if for the obstacles that we cannot walk over.We have to find the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.So, if the input is like −10000000102-1then the output ... Read More
Suppose we have an array A. From some starting index, we can make a series of jumps. The position (1, 3, 5, ...) jumps in the series are called odd numbered jumps, and position (2, 4, 6, ...) jumps in the series are called even numbered jumps.Now we may from index i jump forward to index j (with i < j) in this way −During odd numbered jumps, we can jump to the index j such that A[i] = A[j] and A[j] is the largest possible value. When there are multiple such indexes j, we can only jump to the ... Read More
Suppose we have a binary tree; we place cameras on the nodes of the tree. Now each camera at a node can monitor its parent, itself, and its children. We have to find the minimum number of cameras needed to monitor all nodes of the tree.So, if the input is like −then the output will be 1, because only one camera is enough to track all.To solve this, we will follow these steps −Define one set called covered, of type TreeNode (Tree node has left, right and data field)Define a function solve(), this will take node, parent, if node is ... Read More
Suppose we want to make a target string of lowercase letters.At first, we have the sequence as n '?' marks (n is the length of target string). We also have a stamp of lowercase letters.On each turn, we can place the stamp over the sequence, and replace every letter in the with the corresponding letter from that stamp. You can make up to 10 * n turns. As an example consider the initial sequence is "?????", and the stamp is "abc", then we may make strings like "abc??", "?abc?", "??abc" in the first turn.If the sequence is possible to stamp, ... Read More
Suppose we have a grid. There are few symbols. "." is indicating empty cell, "#" is for wall, "@" is for starting point, ("a", "b", ...) all are keys, and ("A", "B", ...) all are locks. We will start from the starting point, and one move consists of walking one space in one of the 4 directions (left, right, top, bottom). We will not go outside the grid, and there are walls to block our way. If we walk over a key, we pick it up. We can't walk over a lock unless we have the corresponding key.For each lock ... Read More
In this tutorial, we will be discussing a program to find the kth character after decrypting a string.For this, we will be provided with a string that will consist of characters and numbers and integer K. Our task is to decrypt the given string and find the character at Kth position.Example Live Demo#include #include using namespace std; //finding decrypted Kth character char findKthChar(string s, int k) { int len = s.length(); int i = 0; int total_len = 0; while (i < len) { if (isalpha(s[i])) { total_len++; ... Read More
Suppose we have a list of (axis-aligned) rectangles. Here each rectangle[i] = {x1, y1, x2, y2}, where (x1, y1) is the point of the bottom-left corner, and (x2, y2) are the point of the top-right corner of the ith rectangle.We have to find the total area covered by all rectangles in the plane. The answer may be very , so we can use modulo 10^9 + 7.So, if the input is likethen the output will be 6.To solve this, we will follow these steps −m = 10^9 + 7Define a function add(), this will take a, b, return ((a mod ... Read More
Suppose we want to define a function called countUniqueChars(s) that will return the number of unique characters on s, so if s = "HELLOWORLD" then "H", "E", "W", "R", "D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.Now on this problem given a string s we have to find the sum of countUniqueChars(t) where t is a substring of s. (Here some substrings can be repeated so on this case we have to count the repeated ones too.)As the answer can be very large, we can return answer modulo 10^9+7.So, if the input ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP