Fair Candy Swap in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:08:15

534 Views

Suppose A and B are two friends. They have candy bars of different sizes. Here A[i] is the size of the i-th bar of candy owned by A, and B[j] is the size of the j-th bar of candy owned by B.Since they are friends, they want to exchange one candy bar each so that after the exchange, both A and B have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.) We have to return an integer array suppose ans, where ans[0] is ... Read More

Goat Latin in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:55:38

658 Views

Suppose we have a set of strings (Sentence), in that set there are few words. Each words are consists of lowercase and uppercase letters. Our task is to convert the sentence into Goat-Latin form. The Goat Latin is similar to the Pig Latin. There are some few conditions.If the word starts with a vowel, then append ‘ma’ with the wordIt the word starts with a consonant, then remove that from beginning, and append it at the end, then add ‘ma’ at the end.Add one letter ‘a’ to the end of each word per its word index in the sentence, starting ... Read More

Rotate String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:53:02

575 Views

Suppose we have two strings, A and B. We will rotate the string A and check whether it matches with B at any position of rotating, if so then return true, otherwise false. For example, if A = 'abcde', and B = 'bcdea' So answer will be true, as A can be converted to B after rotating it.To solve this, we will follow these steps −When both A and B are empty, then return true, when both are of different length then return falseA := concatenate A after Ai := 0, and j := 0while i < length of Aif ... Read More

Letter Case Permutation in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:50:02

303 Views

Suppose we have a string with letters and numbers. We have to generate all possible combinations of that string by taking uppercase and lowercase versions of letters that are present in the string. So if one string has only numbers, only that will be returned. Suppose the string is like “1ab2”, then the strings will be [“1ab2”, “1Ab2”, “1aB2”, “1AB2”]To solve this problem, we will use recursive approach. It takes the index parameter to start work from that index. It also takes a temp string up to which the result is created. When the index is same as the string ... Read More

Jewels and Stones in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:46:58

790 Views

Suppose we have a string J that indicates some letters that are considered as Jewel, and another string S, that represents some stones that we have. Our task is to find how many of stones in S is also jewel. The letters in J and S are case sensitive. So if the J = “aZc”, and S = “catTableZebraPicnic” then there are 7 jewels.To solve this we will convert the string into a list of characters. If the character in J present in S, then increase the count.ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object): ... Read More

Diameter of Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:41:03

627 Views

Suppose we have a binary tree; we have to compute the length of the diameter of the tree. The diameter of a binary tree is actually the length of the longest path between any two nodes in a tree. This path not necessarily pass through the root. So if the tree is like below, then the diameter will be 3.as the length of the path [4, 2, 1, 3] or [5, 2, 1, 3] is 3To solve this, we will follow these steps −We will use the dfs to find the diameter, set answer := 0call the dfs function with ... Read More

Repeated Substring Pattern in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:38:05

2K+ Views

Suppose we have a non-empty string. We have to check whether it can be constructed by taking a substring of it and appending multiple times of the substring. The string consists of lowercase English letters only and its length will not exceed 10000. So if the input is like “abaabaaba”, then answer will be true, as this is created using “aba”To solve this, we will follow these steps −We will use the dynamic programming approach.Define an array DP of size n. n is the size of the stringi := 1 and j := 0while i < nif str[i] == str[j], ... Read More

Find All Numbers Disappeared in an Array in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:34:03

507 Views

Suppose we have an array of n elements. Some elements appear twice and other appear once. Elements are in range 1 0, then add i + 1 into the answerreturn the answerExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){    cout

Intersection of Two Arrays II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:31:34

5K+ Views

Suppose we have two arrays A and B, there are few elements in these array. We have to find the intersection of them. So if A = [1, 4, 5, 3, 6], and B = [2, 3, 5, 7, 9], then intersection will be [3, 5]To solve this, we will follow these steps −Take two arrays A and Bif length of A is smaller than length of B, then swap themcalculate the frequency of elements in the array and store them into mfor each element e in B, if e is present in m, and frequency is non-zero, decrease frequency ... Read More

Reverse Vowels of a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:27:41

5K+ Views

Suppose we have a lowercase string. Our task is to reverse the vowels present in the string. So if the string is “hello”, then the string after vowel reversal will be “holle”. For string “programming”, it will be “prigrammong”To solve this, we will follow these steps −Take the string and make a list of vowels, and store their indices as wellreverse the vowel listset idx := 0for i := 0 to length of given string – 1if i is in index list −put vowels[i] into final stringidx := idx + 1otherwise put string[i] into final stringreturn the list as a ... Read More

Advertisements