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 <bbj1979@gmail.com>

* Update Strings/CountHomogenous.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/Strings/StringsTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update StringsTest.php

* Update Strings/CountHomogenous.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/Strings/StringsTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* 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 <bbj1979@gmail.com>
This commit is contained in:
niharikamahajan02 2024-06-14 10:56:17 +05:30 committed by GitHub
parent aa72f5558a
commit afc6d11844
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 0 deletions

View File

@ -87,6 +87,7 @@
* [Checkpalindrome](./Strings/CheckPalindrome.php) * [Checkpalindrome](./Strings/CheckPalindrome.php)
* [Checkpalindrome2](./Strings/CheckPalindrome2.php) * [Checkpalindrome2](./Strings/CheckPalindrome2.php)
* [Countconsonants](./Strings/CountConsonants.php) * [Countconsonants](./Strings/CountConsonants.php)
* [Counthomogenous](./Strings/CountHomogenous.php)
* [Countsentences](./Strings/CountSentences.php) * [Countsentences](./Strings/CountSentences.php)
* [Countvowels](./Strings/CountVowels.php) * [Countvowels](./Strings/CountVowels.php)
* [Distance](./Strings/Distance.php) * [Distance](./Strings/Distance.php)

View File

@ -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;
}

View File

@ -8,6 +8,7 @@ require_once __DIR__ . '/../../Strings/CheckAnagram.php';
require_once __DIR__ . '/../../Strings/CheckPalindrome.php'; require_once __DIR__ . '/../../Strings/CheckPalindrome.php';
require_once __DIR__ . '/../../Strings/CheckPalindrome2.php'; require_once __DIR__ . '/../../Strings/CheckPalindrome2.php';
require_once __DIR__ . '/../../Strings/CountConsonants.php'; require_once __DIR__ . '/../../Strings/CountConsonants.php';
require_once __DIR__ . '/../../Strings/CountHomogenous.php';
require_once __DIR__ . '/../../Strings/CountSentences.php'; require_once __DIR__ . '/../../Strings/CountSentences.php';
require_once __DIR__ . '/../../Strings/CountVowels.php'; require_once __DIR__ . '/../../Strings/CountVowels.php';
require_once __DIR__ . '/../../Strings/Distance.php'; require_once __DIR__ . '/../../Strings/Distance.php';
@ -81,6 +82,11 @@ class StringsTest extends TestCase
$this->assertEquals(7, countConsonants("hello world")); $this->assertEquals(7, countConsonants("hello world"));
$this->assertEquals(9, countConsonants("Just A list of somE aaaaaaaaaa")); $this->assertEquals(9, countConsonants("Just A list of somE aaaaaaaaaa"));
} }
public function testCountHomogenous()
{
$this->assertEquals(4, countHomogenous("abbcccaa"));
$this->assertEquals(2, countHomogenous("xy"));
}
public function testFindDistance() public function testFindDistance()
{ {