File tree 3 files changed +39
-0
lines changed 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 87
87
* [ Checkpalindrome] ( ./Strings/CheckPalindrome.php )
88
88
* [ Checkpalindrome2] ( ./Strings/CheckPalindrome2.php )
89
89
* [ Countconsonants] ( ./Strings/CountConsonants.php )
90
+ * [ Counthomogenous] ( ./Strings/CountHomogenous.php )
90
91
* [ Countsentences] ( ./Strings/CountSentences.php )
91
92
* [ Countvowels] ( ./Strings/CountVowels.php )
92
93
* [ Distance] ( ./Strings/Distance.php )
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * Count homogenous substrings
5
+ * @param String $s
6
+ * @return Integer
7
+ */
8
+ function countHomogenous ($ s )
9
+ {
10
+ // Initialize the count of homogeneous substrings
11
+ $ count = 0 ;
12
+
13
+ // Length of the string
14
+ $ length = strlen ($ s );
15
+
16
+ if ($ length == 0 ) {
17
+ return 0 ; // If the string is empty, return 0
18
+ }
19
+
20
+ // Initialize the count of homogeneous substrings
21
+ $ count = 1 ; // Start with 1 since the first character itself starts a substring
22
+
23
+ // Loop through each character in the string, starting from the second character
24
+ for ($ i = 1 ; $ i < $ length ; $ i ++) {
25
+ // Check if current character is not the same as the previous one
26
+ if ($ s [$ i ] != $ s [$ i - 1 ]) {
27
+ $ count ++; // A new substring starts, increment the count
28
+ }
29
+ }
30
+
31
+ return $ count ;
32
+ }
Original file line number Diff line number Diff line change 8
8
require_once __DIR__ . '/../../Strings/CheckPalindrome.php ' ;
9
9
require_once __DIR__ . '/../../Strings/CheckPalindrome2.php ' ;
10
10
require_once __DIR__ . '/../../Strings/CountConsonants.php ' ;
11
+ require_once __DIR__ . '/../../Strings/CountHomogenous.php ' ;
11
12
require_once __DIR__ . '/../../Strings/CountSentences.php ' ;
12
13
require_once __DIR__ . '/../../Strings/CountVowels.php ' ;
13
14
require_once __DIR__ . '/../../Strings/Distance.php ' ;
@@ -81,6 +82,11 @@ public function testCountConsonants()
81
82
$ this ->assertEquals (7 , countConsonants ("hello world " ));
82
83
$ this ->assertEquals (9 , countConsonants ("Just A list of somE aaaaaaaaaa " ));
83
84
}
85
+ public function testCountHomogenous ()
86
+ {
87
+ $ this ->assertEquals (4 , countHomogenous ("abbcccaa " ));
88
+ $ this ->assertEquals (2 , countHomogenous ("xy " ));
89
+ }
84
90
85
91
public function testFindDistance ()
86
92
{
You can’t perform that action at this time.
0 commit comments