Last active
December 16, 2015 02:19
-
-
Save iamshanto/5361198 to your computer and use it in GitHub Desktop.
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
<?php | |
class StringBalance { | |
private $startBrace = "("; | |
private $endBrace = ")"; | |
public $debug = false; | |
private $startBraceFound; | |
private $endBraceFound; | |
private $startBraceCharPositions; | |
private $endBraceCharPositions; | |
private $string; | |
public function __construct($string = '') | |
{ | |
if ($this->debug) { | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
} | |
if (!empty($string)) { | |
$this->string = $string; | |
} | |
$this->cleanString(); | |
} | |
private function cleanString() | |
{ | |
$this->string = preg_replace("/[^\(|\)]/", '', $this->string); | |
} | |
private function setCharPositions() | |
{ | |
$x = count_chars($this->string, 1); | |
$startBraceFound = $x[ord($this->startBrace)]; | |
$endBraceFound = $x[ord($this->endBrace)]; | |
$startBraceCharPositions = array(); | |
$endBraceCharPositions = array(); | |
$lastStrPos = 0; | |
for ($i=1; $i <= $startBraceFound; $i++) { | |
if(($strpos = strpos($this->string, $this->startBrace, $lastStrPos)) !== false) { | |
$lastStrPos = $strpos+1; | |
$startBraceCharPositions[] = $strpos; | |
} | |
} | |
$lastStrPos = 0; | |
for ($i=1; $i <= $endBraceFound; $i++) { | |
if(($strpos = strpos($this->string, $this->endBrace, $lastStrPos)) !== false) { | |
$lastStrPos = $strpos+1; | |
$endBraceCharPositions[] = $strpos; | |
} | |
} | |
$this->startBraceFound = $startBraceFound; | |
$this->endBraceFound = $endBraceFound; | |
$this->startBraceCharPositions = $startBraceCharPositions; | |
$this->endBraceCharPositions = $endBraceCharPositions; | |
} | |
private function setCharPositions2() | |
{ | |
$chars = str_split($this->string); | |
$startBraceCharPositions = array(); | |
$endBraceCharPositions = array(); | |
foreach ($chars as $key => $value) { | |
switch ($value) { | |
case $this->startBrace: | |
$startBraceCharPositions[] = $key; | |
break; | |
case $this->endBrace: | |
$endBraceCharPositions[] = $key; | |
break; | |
} | |
} | |
$this->startBraceFound = count($startBraceCharPositions); | |
$this->endBraceFound = count($endBraceCharPositions); | |
$this->startBraceCharPositions = $startBraceCharPositions; | |
$this->endBraceCharPositions = $endBraceCharPositions; | |
} | |
private function checkBraces() | |
{ | |
if($this->startBraceFound !== $this->endBraceFound || empty($this->startBraceCharPositions)) { | |
return false; | |
} | |
foreach ($this->startBraceCharPositions as $key => $value) { | |
if ($this->startBraceCharPositions[$key] > $this->endBraceCharPositions[$key]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public function isBalanced() | |
{ | |
if (empty($this->string)) { | |
return true; | |
} | |
$this->setCharPositions(); | |
return $this->checkBraces(); | |
} | |
} | |
$str = "Hell(o Wor)ld"; | |
$x = new StringBalance($str); | |
var_dump($x->isBalanced()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment