Suppose we have a string containing digits from 2-9 inclusive. We have to return all possible letter combinations that the number could represent. One mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.12a b c3d e f4g h i5j k l6m n o7p q r s8t u v9w x y z*0#For an example, if the given string is “23”, then the possible strings will be [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]To solve this, we will follow these steps −Define an array called solve ... Read More
Suppose we have an array of numbers. It stores n integers, there are there elements a, b, c in the array, such that a + b + c = 0. Find all unique triplets in the array which satisfies the situation. So if the array is like [-1, 0, 1, 2, -1, -4], then the result will be [[-1, 1, 0], [-1, -1, 2]]To solve this, we will follow these steps −Sort the array nums, and define an array resfor i in range 0 to length of nums – 3if i > 0 and nums[i] = nums[i - 1], then ... Read More
Given a decimal number n, we have to convert this into Roman numeral. The value n lies in the range 1 to 4000. These are some Roman Numerals.NumberNumeral1I4IV5V9IX10X40XL50L90XC100C400CD500D900CM1000M4000MMMMSo if the number n = 859, its Roman Numeral will be DCCCLIXTo solve this, we will follow these stepsDefine an array to store numeral and corresponding values for the given list. That is called nume arraywe are using a recursive approach, the function decToRom() is used. this is taking nume array and the number num.The decToRom() will be likeif num is not 0, thenmax := find maximum value from nume array that ... Read More
Suppose we have a set of n non-negative integers a1, a2, ..., an, each value represents a point at coordinate (i, a[i]). n vertical lines are present in such a way that the two endpoints of line i is at (i, a[i]) and (i, a[0]). We have to find two lines, which together with x-axis forms one container, so our goal is to find two columns where water volume is max. So if the array is like [1, 8, 6, 2, 5, 4, 8, 3, 7], then it will beIn the shaded part, the height is 7 and there are ... Read More
Suppose we have to design a module, that first discards as many whitespace characters as necessary until the first non-whitespace character is reached. After that, starting from this character, it takes an optional initial plus sign or minus sign followed by as many numerical digits, and interprets them as a numerical value.When the first sequence of non-whitespace characters in str is not a valid integral number, or when no such sequence exists because either str is empty or it contains only whitespaces, no conversion will be performed.So if the input is like “-45”, the output will be -45.To solve this, ... Read More
Suppose the string is like " IWANTTOLEARNCODE". This string is written in a zigzag way on a given number of rows say n. So the pattern is looking like thisITAOWNOERCDALNEWhen we read the line like − "ITAOWNOERCDALNE"So we have to create one module that can perform this kind of operation by taking the string and the number of rows.To solve this, we will follow these stepswhen n = 1, then return screate an array of strings arr of size nrow := 0, and down := truefor i in range 0 to size of string – 1insert s[i] at the end ... Read More
Suppose we have a string S. We have to find the longest palindromic substring in S. We are assuming that the length of the string S is 1000. So if the string is “BABAC”, then the longest palindromic substring is “BAB”.To solve this, we will follow these stepsDefine one square matrix of order same as the length of string, and fill it with FalseSet the major diagonal elements as true, so DP[i, i] = True for all i from 0 to order – 1start := 0for l in range 2 to length of S + 1for i in range 0 ... Read More
Suppose we have a string s that is formed by digits ('0' - '9') and '#'. We have to map s to one English lowercase characters as follows −Characters ('a' to 'i') are represented by ('1' to '9') respectively.Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.We have to find the string formed after mapping. We are taking one assumption that a unique mapping will always exist. So if the input is like “10#11#12”, then it will be “jkab”. As 10# is j, 11# is k, 1 is a and 2 is b.To solve this, we will follow ... Read More
Suppose we have one ‘head’ which is a reference node to a singly-linked list. The value of each node present in the linked list is either 0 or 1. This linked list stores the binary representation of a number. We have to return the decimal value of the number present in the linked list. So if the list is like [1, 0, 1, 1, 0, 1]To solve this, we will follow these steps −x := convert the list elements into an arraythen reverse the list xans := 0, and temp := 1for i in range i := 0, to size ... Read More
Suppose we have one 2D grid of size m x n. We have another variable k. We have to shift the grid k times. The shift operation will be as followsElement at grid G[i, j] moves to G[i, j + 1]Element at grid G[i, n – 1] moves to G[i + 1, 0]Element at grid G[m - 1, n – 1] moves to G[0, 0]So if the grid is like −123456789The output will be −912345678To solve this, we will follow these steps −The shift operation will take the matrix as inputn = number of rows, m := number of columns, ... Read More