-
-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
aa72f55
commit afc6d11
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* Count homogenous substrings | ||
* @param String $s | ||
* @return Integer | ||
*/ | ||
function countHomogenous($s) | ||
{ | ||
// Initialize the count of homogeneous substrings | ||
$count = 0; | ||
|
||
// Length of the string | ||
$length = strlen($s); | ||
|
||
if ($length == 0) { | ||
return 0; // If the string is empty, return 0 | ||
} | ||
|
||
// Initialize the count of homogeneous substrings | ||
$count = 1; // Start with 1 since the first character itself starts a substring | ||
|
||
// Loop through each character in the string, starting from the second character | ||
for ($i = 1; $i < $length; $i++) { | ||
// Check if current character is not the same as the previous one | ||
if ($s[$i] != $s[$i - 1]) { | ||
$count++; // A new substring starts, increment the count | ||
} | ||
} | ||
|
||
return $count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters