Skip to content

add: countSubstrings function implementation #1091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions String/CountSubstrings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @function countSubstrings
* @description Given a string of words or phrases, count the occurrences of a substring
* @param {String} str - The input string
* @param {String} substring - The substring
* @return {Number} - The number of substring occurrences
* @example countSubstrings("This is a string", "is") => 2
* @example countSubstrings("Hello", "e") => 1
*/

const countSubstrings = (str, substring) => {
if (typeof str !== 'string' || typeof substring !== 'string') {
throw new TypeError('Argument should be string')
}

if (substring.length === 0) return str.length + 1

let count = 0
let position = str.indexOf(substring)

while (position > -1) {
count++
position = str.indexOf(substring, position + 1)
}

return count
}

export { countSubstrings }
52 changes: 52 additions & 0 deletions String/test/CountSubstrings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { countSubstrings } from '../CountSubstrings'

describe('CountSubstrings', () => {
it('count multiple occurrences of substring in a string', () => {
const str = 'This is a string'
const substring = 'is'
const count = countSubstrings(str, substring)
expect(count).toBe(2)
})

it('should return 0 when input substring has no occurrences', () => {
const str = 'Jurassic Park'
const substring = 'World'
const count = countSubstrings(str, substring)
expect(count).toBe(0)
})

it('should return 1 when input substring is of length 1 that is equal to string', () => {
const str = 's'
const substring = 's'
const count = countSubstrings(str, substring)
expect(count).toBe(1)
})

it('should return the correct result when input string contains spaces', () => {
const str = 'ab cd ef ghi'
const substring = ' '
const count = countSubstrings(str, substring)
expect(count).toBe(4)
})

it('should return the correct result when input substring contains number or special characters', () => {
const str = 'abc1@2def1@2'
const substring = '1@2'
const count = countSubstrings(str, substring)
expect(count).toBe(2)
})

it('should return string.length + 1 when the input substring is an empty string', () => {
const str = 'empty'
const substring = ''
const count = countSubstrings(str, substring)
expect(count).toBe(6)
})

it('should return correct result when input is overlapping substring', () => {
const str = 'aaa'
const substring = 'aa'
const count = countSubstrings(str, substring)
expect(count).toBe(2)
})
})