Encode and Decode Strings in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:44:30

5K+ Views

Suppose we have a list of strings. We have to design an algorithm that can encode the list of strings to a string. We also have to make one decoder that will decode back to the original list of strings. Suppose we have the encoder and decoder installed on these machines, and there are two different functions as follows −Machine 1 (sender) has the functionstring encode(vector

Palindrome Permutation II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:41:53

251 Views

Suppose we have a string s, we have to find all the palindromic permutations of it, there will be no repetition. If no palindromic permutation is there, then simply return empty string.So, if the input is like "aabb", then the output will be ["abba", "baab"]To solve this, we will follow these steps −Define an array retDefine a function solve(), this will take s, sz, one unordered map m, idx initialize it with 0, if sz is same as 0, then −insert s at the end of retreturnevenFound := falseDefine one set visitedfor each key-value pair it of m, do −if ... Read More

Graph Valid Tree in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:39:10

346 Views

Suppose we have n nodes they are labeled from 0 to n-1 and a list of undirected edges [u, v], We have to define a function to check whether these edges make up a valid tree or not.So, if the input is like n = 5, and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], then the output will be trueTo solve this, we will follow these steps −Define a function dfs(), this will take node, par, graph, and another array called visited, if visited[node] is same as 1, then −return trueif visited[node] is same as 2, then ... Read More

Factor Combinations in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:33:01

319 Views

Suppose we have a number. The numbers can be regarded as a product of its factors. So, 8 = 2 x 2 x 2; = 2 x 4. We have to make one function that takes an integer n and return all possible combinations of its factors.So, if the input is like 12, then the output will be [[2, 6], [2, 2, 3], [3, 4]]To solve this, we will follow these steps −Define a function solve(), this will take n, target, start, define one list of lists called retif n is same as 1, then −return retif n is not ... Read More

Meeting Rooms II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:30:42

1K+ Views

Suppose there is an array of meeting time intervals. There are two times start and end times [[s1,e1],[s2,e2],...] and each pair satisfies the rule (si < ei), We have to find the minimum number of conference rooms required.So, if the input is like [[0, 30], [5, 10], [15, 20]], then the output will be 2.To solve this, we will follow these steps −define one priority queue pqsort the intervals arrayret := 0for initialize i := 0, when i < size of intervals, update (increase i by 1), do −while (not pq is empty and top element of pq

Flatten 2D Vector in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:28:57

797 Views

Suppose we have a 2D vector, we have to design and implement an iterator to flatten that 2d vector. There will be different methods as follows −next() − This will return the next element of the current elementhasNext() − This will check whether next element is present or notSo, if the input is like [[1, 2], [3], [4]] then if we call the functions as follows −iterator.next();iterator.next();iterator.next();iterator.hasNext();iterator.hasNext();iterator.next();iterator.hasNext();then the output will be [1, 2, 3, true, true, 4, false]To solve this, we will follow these steps −Define one 2D array vDefine initializer this will take one 2D array v, rowPointer := ... Read More

Count Univalue Subtrees in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:26:39

317 Views

Suppose we have a binary tree; we have to count the number of uni-value subtrees. Here the Uni-value subtree indicates all nodes of the subtree have the same value.So, if the input is like root = [5, 1, 5, 5, 5, null, 5], then the output will be 4To solve this, we will follow these steps −Define a function solve(), this will take node, if node is empty, then −return trueleft := solve(left of node)right := solve(right of node)if left is false or right is false, then −return falseif left of node is present and val of node is not ... Read More

Group Shifted Strings in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:23:44

224 Views

Suppose we have a string, we can "shift" each of its letter to its successive letter, so: "abc" can be changed to "bcd". We can keep doing this operation which forms the sequence: "abc" -> "bcd" -> ... -> "xyz". If we have a list of non-empty strings that contains only lowercase alphabets, we have to group all strings that belong to the same shifting sequence.So, if the input is like ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], then the output will be [ ["abc", "bcd", "xyz"], ["az", "ba"], ["acef"], ["a", "z"] ]To solve this, we will follow these ... Read More

Strobogrammatic Number II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:21:46

506 Views

Suppose we have a length n. We have to find all strobogrammatic numbers that are of length n.As we know that a strobogrammatic number is a number that looks the same when rotated 180 degrees.So, if the input is like n = 2, then the output will be ["11", "69", "88", "96"]To solve this, we will follow these steps −Define an array retif n is odd, then −insert "0" at the end of retinsert "1" at the end of retinsert "8" at the end of retOtherwiseinsert blank string at the end of retfor n > 1, update n := n ... Read More

Shortest Word Distance III in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:19:46

252 Views

Suppose we have a list of words and another two words called word1 and word2, we have to find the shortest distance between these two words in the list. Here word1 and word2 may be the same and they represent two individual words in the list. Let us assume that words = ["practice", "makes", "perfect", "skill", "makes"].So, if the input is like word1 = “makes”, word2 = “skill”, then the output will be 1To solve this, we will follow these steps −ret := 10^9, l1 := 10^9, l2 := -10^9n := size of wordsfor initialize i := 0, when i ... Read More

Advertisements