-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_usingstack.php
More file actions
53 lines (49 loc) · 893 Bytes
/
Copy pathreverse_usingstack.php
File metadata and controls
53 lines (49 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
## Reverse String Problem Using stacks
$string="My name is Akshay";
Reverse($string);
function Reverse($str)
{
$final=Array();
for($i=0;$i<strlen($str);$i++)
{
array_push($final, $str[$i]);
}
for($i=0;$i<strlen($str);$i++)
{
echo array_pop($final);
}
echo '<pre>';print_r($final);
}
## balance parenthasis Problems:
$string="[{()([])}]";
Reverse1($string);
function Reverse1($str)
{
$final=Array();
$match=array('['=>']','('=>')','{'=>'}');
echo '<pre>';print_r($match);
$j=-1;
for($i=0;$i<strlen($str);$i++)
{
if($str[$i]=='[' || $str[$i]=='{'||$str[$i]=='(')
{
array_push($final, $str[$i]);
$j++;
}
elseif($str[$i]==']' || $str[$i]=='}'||$str[$i]==')')
{
if($match[$final[$j]]==$str[$i])
{
array_pop($final);
$j--;
}
}
}
if(count($final))
echo 'NOT Balanced';
else
echo 'Balanced';
echo '<pre>';print_r($final);
}
?>