Found 26504 Articles for Server Side Programming

Longest Repeating Substring in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:28:46

2K+ Views

Suppose we have a string S, we have to find the length of the longest repeating substring(s). We will return 0 if no repeating substring is present. So if the string is like “abbaba”, then the output will be 2. As the longest repeating substring is “ab” or “ba”.Return all words that can be formed in this manner, in lexicographical order.To solve this, we will follow these steps −n := size of Sset S := one blank space concatenated with Sset ret := 0create one matrix dp of size (n + 1) x (n + 1)for i in range 1 ... Read More

Lexicographically Smallest Equivalent String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:29:20

760 Views

Suppose we have strings A and B of the same length, now we can say A[i] and B[i] are equivalent characters. So for example, if A = "abc" and B = "cde", then we have 'a' = 'c', 'b' = 'd' and 'c' = 'e'. The equivalent characters follow the usual rules of any equivalence relation:Reflexivity: 'a' = 'a'Symmetry: 'a' = 'b' implies 'b' = 'a'Transitivity: 'a' = 'b' and 'b' = 'c' implies 'a' = 'c'Now for example, given the equivalency information from A and B above, S = "eed", "acd", and "aab" are equivalent strings, and "aab" is ... Read More

Missing Element in Sorted Array in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:18:32

445 Views

Suppose we have a sorted array A of unique numbers, we have to find the K-th missing number starting from the leftmost number of the array. So if the array is like [4, 7, 9, 10], and k = 1, then the element will be 5.To solve this, we will follow these steps −n := size of the array, set low := 0 and high := n – 1if nums[n - 1] – nums[0] + 1 – n < k, thenreturn nums[n - 1] + (k – (nums[n - 1] – nums[0] + 1 – n))while low < high – ... Read More

Minimize Rounding Error to Meet Target in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:09:51

442 Views

Suppose we have an array of prices P [p1, p2..., pn] and a target value, we have to round each price Pi to Roundi(Pi) so that the rounded array [Round1(P1), Round2(P2)..., Roundn(Pn)] sums to the given target value. Here each operation Roundi(pi) could be either Floor(Pi) or Ceil(Pi).We have to return the string "-1" if the rounded array is impossible to sum to target. Otherwise, return the smallest rounding error, which will be (as a string with three places after the decimal) defined as −$\displaystyle\sum\limits_{i-1}^n |Round_{i} (???? ) - ????$So if the input is like [“0.700”, “2.800”, “4.900”], and the ... Read More

Shortest Way to Form String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:04:56

327 Views

Suppose we have a string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). So if there is two strings source and target, we have to find the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, then return -1. So if source is “abc” and target is “abcbc”, then the output will be 2.To solve this, we will follow these steps −Define a string called possible, this will take s and t as inputcreate a map mfor each character c in s mark ... Read More

Last Stone Weight II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:01:51

1K+ Views

Suppose we have a collection of rocks, now each rock has a positive integer weight. In each turn, we choose any two rocks and smash them together. If the stones have weights x and y with x = 0, decrease j by 1dp[j] := false when (dp[j] and dp[j – stones[i]]) both are false, otherwise trueif dp[j] is true, then reach := max of reach and jreturn total – (2 * reach)Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {    public:    int lastStoneWeightII(vector& stones) {       ... Read More

Longest String Chain in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:57:50

536 Views

Suppose we have a list of words, here each word consists of lowercase letters. So one word word1 is a predecessor of another word word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For the example of the predecessor is like, "abc" is a predecessor of "abac". Now a word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. We have to find the longest possible length of ... Read More

Robot Bounded In Circle C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:54:21

315 Views

Suppose we have an infinite plane, a robot initially stands at position (0, 0) and faces north. The robot can receive one of three instructions −G − go straight 1 unit;L − turn 90 degrees to the left direction;R − turn 90 degrees to the right direction.The robot performs the instructions given in order, Instructions are repeated forever. We have to check whether there exists a circle in the plane such that the robot never leaves the circle. So if the input is like [GGLLGG], then the answer will be true. from (0, 0) to (0, 2), it will loop ... Read More

Minimum Score Triangulation of Polygon in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:49:19

352 Views

Suppose we have a value N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] are in clockwise order. Now suppose we want to triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation will be the sum of these values over all N-2 triangles in the triangulation. We have to find the smallest possible total score that we can achieve with some triangulation of the polygon. So if the input is [1, 2, 3], then the ... Read More

Uncrossed Lines in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:45:20

235 Views

Suppose we have written the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that −A[i] == B[j];The line we draw that does not intersect any other connecting (non-horizontal) line.We have to keep in mind that connecting lines cannot intersect even at the endpoints − each number can only belong to one connecting line. Find the maximum number of connecting lines. So if the input is like [1, 4, 2] and [1, 2, 4], then the output ... Read More

Advertisements