Skip to content

Commit 337c366

Browse files
committed
Remove All Adjacent Duplicates In String
1 parent d4e4d8d commit 337c366

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Hakam\LeetCodePhp\String;
4+
5+
class RemoveAllAdjacentDuplicatesInString
6+
{
7+
8+
/**
9+
* @param String $s
10+
* @return String
11+
*/
12+
function removeDuplicates($s)
13+
{
14+
$stack = [null];
15+
for ($index = 0, $indexMax = strlen($s); $index < $indexMax; $index++) {
16+
17+
if (end($stack) === $s[$index]) {
18+
array_pop($stack);
19+
} else {
20+
$stack [] = $s[$index];
21+
}
22+
}
23+
return implode('', $stack);
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"testCase1": {
3+
"expectedResult": "ca",
4+
"input": "abbaca"
5+
},
6+
"testCase2": {
7+
"expectedResult": "ay",
8+
"input": "azxxzy"
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Hakam\LeetCodePhp\Tests\String;
4+
5+
use Hakam\LeetCodePhp\String\RemoveAllAdjacentDuplicatesInString;
6+
use Hakam\LeetCodePhp\Tests\Helper\MainTest;
7+
8+
class RemoveAllAdjacentDuplicatesInStringTest extends MainTest
9+
{
10+
/**
11+
* @dataProvider provideData
12+
*/
13+
public function testWithDataList($expectedResult, $inputData): void
14+
{
15+
$remover = new RemoveAllAdjacentDuplicatesInString();
16+
$value =$remover->removeDuplicates($inputData);
17+
self::assertEquals($expectedResult,$value);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)