Skip to content

Commit 6c718c0

Browse files
authored
add: countSubstrings function implementation (TheAlgorithms#1091)
1 parent 61c9e8b commit 6c718c0

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

String/CountSubstrings.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @function countSubstrings
3+
* @description Given a string of words or phrases, count the occurrences of a substring
4+
* @param {String} str - The input string
5+
* @param {String} substring - The substring
6+
* @return {Number} - The number of substring occurrences
7+
* @example countSubstrings("This is a string", "is") => 2
8+
* @example countSubstrings("Hello", "e") => 1
9+
*/
10+
11+
const countSubstrings = (str, substring) => {
12+
if (typeof str !== 'string' || typeof substring !== 'string') {
13+
throw new TypeError('Argument should be string')
14+
}
15+
16+
if (substring.length === 0) return str.length + 1
17+
18+
let count = 0
19+
let position = str.indexOf(substring)
20+
21+
while (position > -1) {
22+
count++
23+
position = str.indexOf(substring, position + 1)
24+
}
25+
26+
return count
27+
}
28+
29+
export { countSubstrings }

String/test/CountSubstrings.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { countSubstrings } from '../CountSubstrings'
2+
3+
describe('CountSubstrings', () => {
4+
it('count multiple occurrences of substring in a string', () => {
5+
const str = 'This is a string'
6+
const substring = 'is'
7+
const count = countSubstrings(str, substring)
8+
expect(count).toBe(2)
9+
})
10+
11+
it('should return 0 when input substring has no occurrences', () => {
12+
const str = 'Jurassic Park'
13+
const substring = 'World'
14+
const count = countSubstrings(str, substring)
15+
expect(count).toBe(0)
16+
})
17+
18+
it('should return 1 when input substring is of length 1 that is equal to string', () => {
19+
const str = 's'
20+
const substring = 's'
21+
const count = countSubstrings(str, substring)
22+
expect(count).toBe(1)
23+
})
24+
25+
it('should return the correct result when input string contains spaces', () => {
26+
const str = 'ab cd ef ghi'
27+
const substring = ' '
28+
const count = countSubstrings(str, substring)
29+
expect(count).toBe(4)
30+
})
31+
32+
it('should return the correct result when input substring contains number or special characters', () => {
33+
const str = 'abc1@2def1@2'
34+
const substring = '1@2'
35+
const count = countSubstrings(str, substring)
36+
expect(count).toBe(2)
37+
})
38+
39+
it('should return string.length + 1 when the input substring is an empty string', () => {
40+
const str = 'empty'
41+
const substring = ''
42+
const count = countSubstrings(str, substring)
43+
expect(count).toBe(6)
44+
})
45+
46+
it('should return correct result when input is overlapping substring', () => {
47+
const str = 'aaa'
48+
const substring = 'aa'
49+
const count = countSubstrings(str, substring)
50+
expect(count).toBe(2)
51+
})
52+
})

0 commit comments

Comments
 (0)