Skip to content

Commit afc6d11

Browse files
Added code for findng no of homogenous substrings (#146)
* Create CountHomogenous.php Added count no of homogenous substrings * Added program for finding no. of homogenous substrings * Update Strings/CountHomogenous.php Co-authored-by: Brandon Johnson <[email protected]> * Update Strings/CountHomogenous.php Co-authored-by: Brandon Johnson <[email protected]> * Update tests/Strings/StringsTest.php Co-authored-by: Brandon Johnson <[email protected]> * Update StringsTest.php * Update Strings/CountHomogenous.php Co-authored-by: Brandon Johnson <[email protected]> * Update tests/Strings/StringsTest.php Co-authored-by: Brandon Johnson <[email protected]> * Update CountHomogenous.php * Update tests/Strings/StringsTest.php * Update tests/Strings/StringsTest.php * Fix homogenous count unit test * Fix count homogenous substrings algorithm * PHPCBF: remove inline control structure --------- Co-authored-by: Brandon Johnson <[email protected]>
1 parent aa72f55 commit afc6d11

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
* [Checkpalindrome](./Strings/CheckPalindrome.php)
8888
* [Checkpalindrome2](./Strings/CheckPalindrome2.php)
8989
* [Countconsonants](./Strings/CountConsonants.php)
90+
* [Counthomogenous](./Strings/CountHomogenous.php)
9091
* [Countsentences](./Strings/CountSentences.php)
9192
* [Countvowels](./Strings/CountVowels.php)
9293
* [Distance](./Strings/Distance.php)

Strings/CountHomogenous.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

tests/Strings/StringsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require_once __DIR__ . '/../../Strings/CheckPalindrome.php';
99
require_once __DIR__ . '/../../Strings/CheckPalindrome2.php';
1010
require_once __DIR__ . '/../../Strings/CountConsonants.php';
11+
require_once __DIR__ . '/../../Strings/CountHomogenous.php';
1112
require_once __DIR__ . '/../../Strings/CountSentences.php';
1213
require_once __DIR__ . '/../../Strings/CountVowels.php';
1314
require_once __DIR__ . '/../../Strings/Distance.php';
@@ -81,6 +82,11 @@ public function testCountConsonants()
8182
$this->assertEquals(7, countConsonants("hello world"));
8283
$this->assertEquals(9, countConsonants("Just A list of somE aaaaaaaaaa"));
8384
}
85+
public function testCountHomogenous()
86+
{
87+
$this->assertEquals(4, countHomogenous("abbcccaa"));
88+
$this->assertEquals(2, countHomogenous("xy"));
89+
}
8490

8591
public function testFindDistance()
8692
{

0 commit comments

Comments
 (0)