Web Development Articles

Page 244 of 801

Longest string with two distinct characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 443 Views

We need to write a JavaScript function that finds the longest substring containing at most two distinct characters. The function takes a string as input and returns the length of the longest valid substring. For example, if the input string is 'kjeljsdl', the longest substring with at most 2 distinct characters is 'jljl' with length 4. Understanding the Problem A substring with at most two distinct characters means we can have: Only one character repeated: "aaa" Two different characters in any combination: "abab", "aabb" Sliding Window Approach The most efficient solution uses a ...

Read More

Substring combination in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

We are required to write a JavaScript function that takes in two strings as the first and the second argument. Let's call these strings str1 and str2. The function should check whether there exists a substring combination in str2, that when combined yields str2. By substring combination, we mean that we can skip characters but we have to maintain the order of the characters selected from str1. For example, if the input strings are: const str1 = 'desxooajmepwele'; const str2 = 'example'; Then the output should be true because the string 'example' can be ...

Read More

Determining beautiful number string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

A numeric string is called a beautiful string if it can be split into a sequence of two or more positive integers, satisfying the following conditions: arr[i] - arr[i - 1] = 1, for any i in the sequence, i.e., each element is exactly one more than the previous element. No element should contain a leading zero. For example, '50607' can be split into [5, 06, 07], but it's not beautiful because 06 and 07 have leading zeros. The sequence order cannot be rearranged. Example For ...

Read More

Finding minimum deletions in string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

When working with binary strings, we often need to ensure no two adjacent characters are the same. This requires finding the minimum number of deletions to create an alternating pattern. Problem Statement Given a binary string, we need to find the minimum deletions required so that no two adjacent characters are identical. const str = '001001'; console.log("Original string:", str); Original string: 001001 For the string '001001', we need 2 deletions to get '0101' (removing characters at indices 0 and 3). Solution Approach The algorithm iterates through the string and ...

Read More

Finding three elements with required sum in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should then pick three such numbers from the array, (if they exist) whose sum is equal to the number specified by the second argument. The function should finally return an array of arrays of all such triplets if they exist, an empty array otherwise. For example − If the input array and the number is − const arr = [2, 5, 7, 8, 9, 11, 1, ...

Read More

Adding paragraph tag to substrings within a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 658 Views

We are required to write a JavaScript function that takes in a string str as the first argument and an array of strings, arr as the second argument. We need to add a closed pair of paragraph tag and to wrap the substrings in str that exist in arr. If two such substrings overlap, we need to wrap them together by only one pair of closed paragraph tag. Also, if two substrings wrapped by paragraph tags are consecutive, we need to combine them. For example, if the input string and the array are: const ...

Read More

Checking if an array is sorted lexicographically in reference to some scrambled alphabet sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 363 Views

We need to write a JavaScript function that checks if an array of words is sorted lexicographically according to a custom alphabet order. The function takes two arguments: an array of words and a string representing the custom alphabetical order. The task is to verify whether the words are arranged correctly based on the given alphabet sequence. If they are properly sorted, return true; otherwise, return false. Problem Understanding Consider this example: const arr = ['this', 'is', 'something', 'mad']; const order = 'hdetljnopqabcuvwxfgirsykmz'; In the custom order, 't' comes before 'i', 'i' comes ...

Read More

Intersection of three sorted arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 499 Views

We are required to write a JavaScript function that takes in three arrays of integers all sorted in an increasing order. The function should then construct and return an array that contains only those elements that are present in all three arrays. For example, if the input arrays are: const arr1 = [4, 7, 8, 11, 13, 15, 17]; const arr2 = [1, 3, 4, 13, 18]; const arr3 = [2, 4, 7, 8, 9, 10, 13]; Then the output should be: const output = [4, 13]; Three-Pointer Approach Since ...

Read More

Validating a square in a 2-D plane in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

We are required to write a JavaScript function that takes in four arguments. The four arguments will all be arrays of exactly two numbers representing the coordinates of four vertices of a quadrilateral or any figure (closed or unclosed) on a plane. The task of our function is to determine whether or not the four vertices form a square. If they do form a square, we should return true, false otherwise. Understanding the Problem A square has specific properties: All four sides are equal in length All four angles ...

Read More

Finding maximum number of consecutive 1's in a binary array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 392 Views

We are required to write a JavaScript function that takes in a binary array (an array that consists of 0 or 1 only) as the only argument. The function should find the length of that consecutive subarray of the array that consists of only 1 and return it. For example − If the input array is − const arr = [1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1]; Then the output should be − 4 We will use the sliding window algorithm to ...

Read More
Showing 2431–2440 of 8,010 articles
« Prev 1 242 243 244 245 246 801 Next »
Advertisements