From eb99c1db74d527fecd5727a10dba862fe91c60ff Mon Sep 17 00:00:00 2001 From: ajinkyac03 Date: Tue, 6 Sep 2022 17:54:42 +0530 Subject: [PATCH 1/3] Added countSubstrings function implementation. --- String/CountSubstrings.js | 29 +++++++++++++++++++ String/test/CountSubstrings.test.js | 45 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 String/CountSubstrings.js create mode 100644 String/test/CountSubstrings.test.js diff --git a/String/CountSubstrings.js b/String/CountSubstrings.js new file mode 100644 index 0000000000..a46caeb7c6 --- /dev/null +++ b/String/CountSubstrings.js @@ -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) + } + + return count +} + +export { countSubstrings } diff --git a/String/test/CountSubstrings.test.js b/String/test/CountSubstrings.test.js new file mode 100644 index 0000000000..5b9b0831de --- /dev/null +++ b/String/test/CountSubstrings.test.js @@ -0,0 +1,45 @@ +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) + }) +}) From bf5c4a728d7fb3ef95f15fc7f80dd5562a697c6f Mon Sep 17 00:00:00 2001 From: ajinkyac03 Date: Tue, 6 Sep 2022 18:04:33 +0530 Subject: [PATCH 2/3] Typo fix --- String/test/CountSubstrings.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/test/CountSubstrings.test.js b/String/test/CountSubstrings.test.js index 5b9b0831de..5bf7a5afbc 100644 --- a/String/test/CountSubstrings.test.js +++ b/String/test/CountSubstrings.test.js @@ -1,4 +1,4 @@ -import { countSubstrings } from '../countSubstrings' +import { countSubstrings } from '../CountSubstrings' describe('CountSubstrings', () => { it('count multiple occurrences of substring in a string', () => { From 99f011bc4f38fa1efefcebbfffb4c4ae5e0428a5 Mon Sep 17 00:00:00 2001 From: ajinkyac03 Date: Tue, 6 Sep 2022 18:19:11 +0530 Subject: [PATCH 3/3] Review changes and added test case for overlapping substring --- String/CountSubstrings.js | 6 +++--- String/test/CountSubstrings.test.js | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/String/CountSubstrings.js b/String/CountSubstrings.js index a46caeb7c6..f936f8eab0 100644 --- a/String/CountSubstrings.js +++ b/String/CountSubstrings.js @@ -13,14 +13,14 @@ const countSubstrings = (str, substring) => { throw new TypeError('Argument should be string') } - if (substring.length <= 0) return (str.length + 1) + 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) + count++ + position = str.indexOf(substring, position + 1) } return count diff --git a/String/test/CountSubstrings.test.js b/String/test/CountSubstrings.test.js index 5bf7a5afbc..66628b1a14 100644 --- a/String/test/CountSubstrings.test.js +++ b/String/test/CountSubstrings.test.js @@ -42,4 +42,11 @@ describe('CountSubstrings', () => { 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) + }) })

AltStyle によって変換されたページ (->オリジナル) /