diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index a62299ae..33683037 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -440,6 +440,7 @@ - [Replace every nth](replace-every-nth "57fcaed83206fb15fd00027a") - [Resistor Color Codes](resistor-color-codes "57cf3dad05c186ba22000348") - [Responsible Drinking](responsible-drinking "5aee86c5783bb432cd000018") +- [Return substring instance count](return-substring-instance-count "5168b125faced29f66000005") - [Return the first M multiples of N](return-the-first-m-multiples-of-n "593c9175933500f33400003e") - [Reverse a Number](reverse-a-number "555bfd6f9f9f52680f0000c5") - [Reverse the bits in an integer](reverse-the-bits-in-an-integer "5959ec605595565f5c00002b") diff --git a/kata/7-kyu/return-substring-instance-count/README.md b/kata/7-kyu/return-substring-instance-count/README.md new file mode 100644 index 00000000..b6270ca4 --- /dev/null +++ b/kata/7-kyu/return-substring-instance-count/README.md @@ -0,0 +1,17 @@ +# [Return substring instance count](https://www.codewars.com/kata/return-substring-instance-count "https://www.codewars.com/kata/5168b125faced29f66000005") + +Write a function that takes two string parameters `search_text` and `full_text` and returns the number of times the `search_text` is found +within the `full_text`. + +* Overlap is not permitted: `"aaa"` contains `1` instance of `"aa"`, not `2`. +* `search_text` will never be empty. + +## Examples: + +``` +full_text = "aa_bb_cc_dd_bb_e", search_text = "bb" +-- > should return 2 since "bb" shows up twice + +full_text = "aaabbbcccc", search_text = "bbb" +-- > should return 1 +``` \ No newline at end of file diff --git a/kata/7-kyu/return-substring-instance-count/main/Solution.java b/kata/7-kyu/return-substring-instance-count/main/Solution.java new file mode 100644 index 00000000..ef41816a --- /dev/null +++ b/kata/7-kyu/return-substring-instance-count/main/Solution.java @@ -0,0 +1,5 @@ +interface Solution { + static int substringCount(String fullText, String search) { + return (fullText + "_").split(search).length - 1; + } +} \ No newline at end of file diff --git a/kata/7-kyu/return-substring-instance-count/test/SolutionTest.java b/kata/7-kyu/return-substring-instance-count/test/SolutionTest.java new file mode 100644 index 00000000..82b5c54b --- /dev/null +++ b/kata/7-kyu/return-substring-instance-count/test/SolutionTest.java @@ -0,0 +1,18 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(delimiter = '|', textBlock = """ + abcdeb | b | 2 + abcdeb | a | 1 + ccddeeccddeecc | cc | 3 + aaabbbccc | bb | 1 + ,,,..239,,,,,., | ,, | 3 + """) + void sample(String fullText, String search, int expected) { + assertEquals(expected, Solution.substringCount(fullText, search)); + } +} \ No newline at end of file