Finding GCD of Two Strings in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:32:38

3K+ Views

In the number system, the Greatest Common Divisor (GCD) of two numbers is that greatest number which divides both the numbers. Similarly, if we apply this concept to strings, the gcd of two strings is the greatest substring (greatest in length) that is present in both the strings.For example −If the two strings are −const str1 = 'abcabc'; const str2 = 'abc';Then the gcd of these strings will be −const gcd = 'abc';We are required to write a JavaScript function that takes in two strings str1 and str2 and computes and returns their gcd.ExampleThe code for this will be − Live ... Read More

Finding All Valid Word Squares in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:29:33

272 Views

Word Square:A word square consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically.For instance, once valid word square is −H E A R T E M B E R A B U S E R E S I N T R E N DWe are required to write a JavaScript function that takes in an array of words. The function should return true if the array given as input forms a valid word square, false otherwise.For example −If the input word array is −const arr ... Read More

Find Smallest Letter Greater Than Target in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:27:05

430 Views

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target.We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target.We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.For Example −If the input array and letter are −const arr = ... Read More

Counting All Possible Palindromic Subsequences in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:24:49

442 Views

Palindrome Sequence:A string sequence is known as palindrome sequence if it reads the same from front and the back. For instance, 'aba', 'madam, 'did' are all valid palindrome sequences.We are required to write a JavaScript function that takes in a string as the first and the only argument. The string taken as input is guaranteed to consist of only 'a', 'b', 'c' and 'd'. Our function should count and return the number of all contiguous or noncontiguous palindrome subsequences that appear in the string.For example −If the input string is −const str = 'bccb';Then the output should be −const output ... Read More

Can Array be Divided into N Partitions with Equal Sums in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:22:40

242 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument.The function should determine whether there exists a way of distributing the elements of the array arr into num groups such that all the groups have equal sum. If there exists any such way, our function should return true, false otherwise.For example −If the input array and the number are −const arr = [4, 6, 3, 3, 7, 4, 1]; const num = 4;Then the output should be −const output = true;because the ... Read More

Find N Most Frequent Words from a Sentence in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:08:31

1K+ Views

For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces.We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies.For example −If the input sentence and the number is −const str ... Read More

Check if an Array is Sorted Lexicographically in JavaScript

AmitDiwan
Updated on 26-Feb-2021 10:02:43

324 Views

We are required to write a JavaScript function that takes in an array of string words as the first argument. The second argument to the function will be a string containing all 26 English lowercase alphabets but in some random scrambled order.The task of our function is to check whether the words in the array are placed lexicographically correctly according to the order specified by the second argument. If it is so, we should return true, false otherwise.For example −If the input array of words and the order is −const arr = ['this', 'is', 'something', 'mad']; const order = 'hdetljnopqabcuvwxfgirsykmz';Then ... Read More

Square Every Digit of a Number Using Split in JavaScript

AmitDiwan
Updated on 26-Feb-2021 10:01:17

226 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then square every digit of the number, append them and yield the new number.For example −If the input number is −const num = 12349;Then the output should be −const output = 1491681;because '1' + '4' + '9' + '16' + '81' = 1491681ExampleThe code for this will be − Live Democonst num = 12349; const squareEvery = (num = 1) => {    let res = ''    const numStr = String(num);    const numArr = numStr.split('');   ... Read More

Calculate Time Taken to Type Words in JavaScript

AmitDiwan
Updated on 26-Feb-2021 09:59:42

298 Views

Suppose we have a keyword, which instead of the traditional qwerty type key mapping, maps keys simply according to the english alphabetic order i.e., abcde...Before we dive into the problem, we have to make the following two assumptions −Currently our fingertip is placed at index 0, i.e., the key 'aThe time taken to move from one key to some other is the absolute difference of their index, for example the time taken to move from 'a' to 'k' will be |0 - 10| = 10We are required to write a JavaScript function that takes in a string of english lowercase ... Read More

Map Anagrams to One Another in JavaScript

AmitDiwan
Updated on 26-Feb-2021 09:57:21

212 Views

Anagram arrays:One array is an anagram of another if we can randomise the elements of that array to achieve the other array.For example −[1, 2, 3] and [2, 1, 3] are anagrams of each other.Suppose, we have two arrays, arr1 and arr2 which are anagrams of each other.We are required to write a JavaScript function that takes in these two arrays and returns a new mapping array of the same length as arr1 and arr2. The mapping array should contain the index of elements of the arr1 array as they are present in the arr2 array.For example −If the two ... Read More

Advertisements