Found 7197 Articles for C++

Flatten 2D Vector in C++

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

723 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

275 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

198 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

477 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

225 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

Shortest Word Distance II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:17:13

375 Views

Suppose there is a class that receives a list of words in the constructor, there will be a method that takes two words word1 and word2 and find the shortest distance between these two words in the list. That method will be called repeatedly many times with different parameters.Let us assume that words = ["practice", "makes", "perfect", "skill", "makes"].So, if the input is like word1 = “skill”, word2 = “practice”, then the output will be 3To solve this, we will follow these steps −Define one map mThe initializer takes an array of wordsfor initialize i := 0, when i < ... Read More

Missing Ranges in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:13:29

239 Views

Suppose we have a sorted integer array nums, the range of elements are in the inclusive range [lower, upper], we have to find the missing ranges.So, if the input is like nums = [0, 1, 3, 50, 75], and lower value is 0 and upper value is 99, then the output will be ["2", "4->49", "51->74", "76->99"]To solve this, we will follow these steps −Define an array numsDefine one set vfor initialize i := 0, when i < size of t, update (increase i by 1), do −if t[i] is not in v, then −insert t[i] into vinsert t[i] at ... Read More

One Edit Distance in C++

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

184 Views

Suppose we have two strings s and t; we have to check whether they are both one edit distance apart. The one edit distance has three types −Insert a character into s to get tDelete a character from s to get tReplace a character of s to get tSo, if the input is like s = "ab", t = "acb", then the output will be TrueTo solve this, we will follow these steps −n := size of s, m := size of tif n < m, then −return isOneEditDistance(t, s)for initialize i := 0, when i < m, update (increase ... Read More

Longest Substring with At Most Two Distinct Characters in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:09:04

1K+ Views

Suppose we have a string s; we have to find the length of the longest substring t that has at most 2 distinct characters.So, if the input is like "eceba", then the output will be 3 as t is "ece" which its length is 3.To solve this, we will follow these steps −Define a function lengthOfLongestSubstringKDistinct(), this will take s, k, ans := 0Define one map mn := size of s, x := 0for initialize j := 0, i := 0, when j < n, update (increase j by 1), do −(increase m[s[j]] by 1)if m[s[j]] is same as 1, ... Read More

Binary Tree Upside Down in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:07:24

330 Views

Suppose we have a binary tree where all the right nodes are either leaf nodes with a sibling otherwise empty, we have to flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. We have to return the new node.So, if the input is like [1, 2, 3, 4, 5]then the output will return the root of the binary tree [4, 5, 2, #, #, 3, 1]To solve this, we will follow these steps −Define a function solve(), this will take node, par, sibling, if node is not present, then ... Read More

Advertisements