Programming Articles - Page 1847 of 3366

Substring with Concatenation of All Words in C++ Program

Arnab Chakraborty
Updated on 21-Jul-2020 09:09:36

226 Views

Suppose we have a string, s, and we also have a list of words, words present in the array are all of the same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “barfoothefoobarman” and words are [“foo”, “bar”], then the output will be [0, 9]. This is because the substring starting at index 0 and 9 are “barfoo” and “foobar”.To solve this, we will follow these steps −Define a method called ok(), this will take ... Read More

Encode String with Shortest Length in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:06:42

680 Views

Suppose we have a non-empty string; we have to encode this string such that its encoded length will be minimum.The encoding rule is like − k[encoded_string], where the encoded_string inside [ ] is being repeated exactly k times. We have to keep in mind that k will be a positive integer and encoded string will not be empty or have extra space. We can assume that the input string contains only lowercase letters. If the encoding process does not make the string shorter, then do not encode that string.So, if the input is like "aaaaa", then the output will be ... Read More

Count The Repetitions in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:04:48

329 Views

Suppose we have two non-empty strings s1 and s2 (maximum 100 characters) and two numbers n1 and n2 both are in range 0 to 106. Now suppose the strings S1 and S2, where S1=[s1, n1] and S2=[s2, n2].S = [s, n] defines the string S which consists of n connected strings s. As an exdample, ["ab", 4] ="abababab".On the other hand, we cal also define that string s1 can be obtained from string s2 if we remove some characters from s2 such that it becomes s1. So, "abc" can be obtained from "abdbec" based on the definition, but it can ... Read More

Optimal Account Balancing in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:02:52

537 Views

Suppose a group of friends went on holiday and sometimes they lent each other money. So as an example, Amit paid for Bikram's lunch for $10. Then later Chandan gave Amit $5 for a taxi fare. We have to design a model where each transaction is taken as a tuple (x, y, z) which means person x gave person y $z.Assuming Amit, Bikram, and Chandan are person 0, 1, and 2 respectively the transactions can be represented as [[0, 1, 10], [2, 0, 5]]. If we have a list of transactions between a group of people, we have to find ... Read More

LFU Cache in Python

Arnab Chakraborty
Updated on 21-Jul-2020 09:00:43

2K+ Views

Suppose we want to design and implement a data structure for Least Frequently Used (LFU) cache system. It should support the following operations −get(key) – This will be used to get the value of the key if the key exists in the cache, otherwise return -1.put(key, value) – This will be used to set or insert the value if the key is not already present.When the cache reached its maximum capacity, it should invalidate the least frequently used element before inserting a new element.So, if the LFUCache is initialized with capacity 2 and call these methods cache.put(1, 1); cache.put(2, 2); ... Read More

Encode N-ary Tree to Binary Tree in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:57:15

600 Views

Suppose we have a N-ary tree. We have to encode that tree into one binary. We also have to make deserializer to deserialize the binary tree to N-ary tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function encode(), this will take root, if root is valid, then −return nullnode = new tree node with value of rootif size of children of root is not 0, then −left of node := encode(children[0] of root)curr = left of nodefor initialize i := 1, when i < size of children of root, ... Read More

Serialize and Deserialize N-ary Tree in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:53:52

427 Views

Suppose we have one N-ary tree and we have to serialize and deserialize them. As we know that the serialization is the process of converting a data structure or object into a sequence of bits so we can store them in a file or memory buffer, and that can be reconstructed later in the same or another computer environment.Here we have to devise an algorithm to serialize and deserialize an N-ary tree. The N-ary tree is a rooted tree in which each node has no more than N children.So, if the input is likethen the output will be Serialize: 1 ... Read More

Word Squares in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:49:06

330 Views

Suppose we have a set of words (all are unique), we have to find all word squares and we can build from them. Here a sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < maximum of numRows and numColumns. So as an example, the word sequence ["ball", "area", "lead", "lady"] will construct a word square because each word reads the same both horizontally and vertically.ballarealeadladySo, if the input is like ["area", "lead", "wall", "lady", "ball"], then the output will be [[ "wall", "area", "lead", "lady"], ... Read More

Minimum Unique Word Abbreviation in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:45:17

318 Views

Suppose we have a string such as "word" and that contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]. We also have a target string and a set of strings in a dictionary, we have to find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary. Here each number or letter in the abbreviation is considered as length = 1. So as an example, the abbreviation "a32bc" is of length = 4.So, if ... Read More

Perfect Rectangle in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:46:27

275 Views

Suppose we have N axis-aligned rectangles, we have to check whether they all together form an exact cover of a rectangular region or not. Here each rectangle is represented as a bottom-left point and a top-right point. So a unit square is represented as [1, 1, 2, 2]. (bottom-left point is (1, 1) and top-right point is (2, 2)).So, if the input is like rectangles = [[1, 1, 3, 3], [3, 1, 4, 2], [3, 2, 4, 4], [1, 3, 2, 4], [2, 3, 3, 4]], then the output will be true as all 5 rectangles together form an exact ... Read More

Advertisements