FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
InterviewAlgorithms/BracketMatcher.kt at master · AliAzaz/InterviewAlgorithms · GitHub
AliAzaz
/
InterviewAlgorithms
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
InterviewAlgorithms
/
BracketMatcher.kt
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (36 loc) · 1.16 KB
Breadcrumbs
InterviewAlgorithms
/
BracketMatcher.kt
Copy path
File metadata and controls
41 lines (36 loc) · 1.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Identity weather statement is fulfilling opening and closing brackets or not like when bracket is opening
* then it also has to close otherwise statement is false.
*
*/
fun
main
() {
println
(bracketMatcher02(
"
(coder)(byte)()
"
))
}
fun
bracketMatcher
(
statement
:
String
):
Boolean
{
var
flag
=
true
var
count
=
0
statement.forEach {
if
(it
==
'
(
'
) count
++
else
if
(it
==
'
)
'
)
if
(count
>
0
) count
--
else
{
flag
=
false
return
@forEach
}
}
return
flag
}
fun
bracketMatcher02
(
statement
:
String
):
Boolean
{
val
stack
=
ArrayDeque
<
Char
>()
val
matchingBrackets
=
mapOf
(
'
)
'
to
'
(
'
,
'
}
'
to
'
{
'
,
'
]
'
to
'
[
'
)
statement.forEach {
when
{
it
==
'
(
'
||
it
==
'
{
'
||
it
==
'
[
'
->
stack.addLast(it)
//
Push opening brackets
it
==
'
)
'
||
it
==
'
}
'
||
it
==
'
]
'
->
{
if
(stack.isEmpty()
||
stack.removeLast()
!=
matchingBrackets[it]) {
return
false
//
Unmatched closing bracket or stack underflow
}
}
}
}
return
stack.isEmpty()
//
If stack is empty, brackets are balanced
}
Back
|
FazBrowse Home
|
New Git URL