Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6c718c0

Browse files
authored
add: countSubstrings function implementation (#1091)
1 parent61c9e8b commit6c718c0

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

‎String/CountSubstrings.js

Lines changed: 29 additions & 0 deletions
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+
constcountSubstrings=(str,substring)=>{
12+
if(typeofstr!=='string'||typeofsubstring!=='string'){
13+
thrownewTypeError('Argument should be string')
14+
}
15+
16+
if(substring.length===0)returnstr.length+1
17+
18+
letcount=0
19+
letposition=str.indexOf(substring)
20+
21+
while(position>-1){
22+
count++
23+
position=str.indexOf(substring,position+1)
24+
}
25+
26+
returncount
27+
}
28+
29+
export{countSubstrings}

‎String/test/CountSubstrings.test.js

Lines changed: 52 additions & 0 deletions
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+
conststr='This is a string'
6+
constsubstring='is'
7+
constcount=countSubstrings(str,substring)
8+
expect(count).toBe(2)
9+
})
10+
11+
it('should return 0 when input substring has no occurrences',()=>{
12+
conststr='Jurassic Park'
13+
constsubstring='World'
14+
constcount=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+
conststr='s'
20+
constsubstring='s'
21+
constcount=countSubstrings(str,substring)
22+
expect(count).toBe(1)
23+
})
24+
25+
it('should return the correct result when input string contains spaces',()=>{
26+
conststr='ab cd ef ghi'
27+
constsubstring=' '
28+
constcount=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+
conststr='abc1@2def1@2'
34+
constsubstring='1@2'
35+
constcount=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+
conststr='empty'
41+
constsubstring=''
42+
constcount=countSubstrings(str,substring)
43+
expect(count).toBe(6)
44+
})
45+
46+
it('should return correct result when input is overlapping substring',()=>{
47+
conststr='aaa'
48+
constsubstring='aa'
49+
constcount=countSubstrings(str,substring)
50+
expect(count).toBe(2)
51+
})
52+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp