
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Counting matching substrings in JavaScript
Problem
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. Our function should count and return the number of arr[i] that is a subsequence of string str.
For example, if the input to the function is
Input
const str = 'klmnop'; const arr = ['k', 'll', 'klp', 'klo'];
Output
const output = 3;
Output Explanation
Because the required strings are ‘k’, ‘klp’, and ‘klo’
Example
Following is the code −
const str = 'klmnop'; const arr = ['k', 'll', 'klp', 'klo']; const countSubstrings = (str = '', arr = []) => { const map = arr.reduce((acc, val, ind) => { const c = val[0] acc[c] = acc[c] || [] acc[c].push([ind, 0]) return acc }, {}) let num = 0 for (let i = 0; i < str.length; i++) { if (map[str[i]] !== undefined) { const list = map[str[i]] map[str[i]] = undefined list.forEach(([wordIndex, charIndex]) => { if (charIndex === arr[wordIndex].length - 1) { num += 1 } else { const nextChar = arr[wordIndex][charIndex + 1] map[nextChar] = map[nextChar] || [] map[nextChar].push([wordIndex, charIndex + 1]) } }) } } return num } console.log(countSubstrings(str, arr));
Output
3
- Related Articles
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Counting even decimal value substrings in a binary string in C++
- Implementing counting sort in JavaScript
- Regular Expression Matching in JavaScript
- Counting smaller and greater in JavaScript
- Counting rings in letters using JavaScript
- Add matching object values in JavaScript
- Counting number of 9s encountered while counting up to n in JavaScript
- Unique substrings in circular string in JavaScript
- Group matching element in array in JavaScript
- Counting numbers after decimal point in JavaScript
- Number of letters in the counting JavaScript
- Counting adjacent pairs of words in JavaScript
- Wildcard matching of string JavaScript
- Counting unique elements in an array in JavaScript

Advertisements