Skip to content

Commit

Permalink
added case for check Anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
H-S-prathamesh-parulekar committed Oct 7, 2023
1 parent 9f7292d commit 536f2c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Sorting/BubbleSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ function bubbleSort($array)
$length = count($array);

for ($i = $length; $i > 0; $i--) {
$swapped = true;
$swapped = false;

for ($j = 0; $j < $i - 1; $j++) {
if ($array[$j] > $array[$j + 1]) {
$temp = $array[$j];
$array[$j] = $array[$j + 1];
$array[$j + 1] = $temp;
$swapped = false;
$swapped = true;
}
}

if ($swapped) {
if (!$swapped) {
break;
}
}
Expand Down
8 changes: 5 additions & 3 deletions Strings/CheckAnagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
function isAnagram(string $originalString, string $testString, bool $caseInsensitive = true)
{
if ($caseInsensitive) {
$originalString = strtolower($originalString); // Converting string to lowercase for case-insensitive check
$originalString = strtolower($originalString);
$testString = strtolower($testString);
}

// count_chars(string, mode = 1) returns key-value pairs with character as key, frequency as value
// We can directly compare the arrays in this case
if (strlen($originalString) !== strlen($testString)) {
return false;
}

return count_chars($originalString, 1) === count_chars($testString, 1);
}

0 comments on commit 536f2c4

Please sign in to comment.