Mảng javascript thành chuỗi có dấu ngoặc

Điểm bằng là một chỉ số sao cho số lượng dấu ngoặc mở trước nó bằng số lượng dấu ngoặc đơn đóng từ và sau nó

ví dụ.   

Đầu vào. str = “(())))(“
đầu ra. 4
Giải trình. Sau chỉ mục 4, chuỗi chia thành (()) và ))(. Số lượng ngoặc mở ở phần thứ nhất bằng số lượng ngoặc ở phần thứ hai

Đầu vào. str = “))”
đầu ra. 2
Giải trình. Như sau vị trí thứ 2 tôi. e. )) và chuỗi “rỗng” sẽ được chia thành hai phần này. Vì vậy, trong số lần mở ngoặc này tôi. e. 0 trong phần đầu tiên bằng với số lượng dấu ngoặc đóng trong phần thứ hai i. e. cũng 0

Mảng javascript thành chuỗi có dấu ngoặc

Đã hỏi trong. amazon

Đề nghị thực hành

Điểm bằng nhau trong một chuỗi dấu ngoặc

Thử nó

Cách tiếp cận 1

  • Lưu trữ số lượng dấu ngoặc mở xuất hiện trong chuỗi cho đến mọi chỉ mục, nó phải bắt đầu từ chỉ mục bắt đầu
  • Tương tự, Lưu trữ số lượng dấu ngoặc đóng xuất hiện trong chuỗi cho đến từng chỉ mục nhưng phải thực hiện từ chỉ mục cuối cùng
  • Kiểm tra xem có bất kỳ chỉ mục nào có cùng giá trị với mở và đóng ngoặc không

Dưới đây là việc thực hiện các phương pháp trên

C++




// C++ program to find an index k which

// decides the number of opening brackets

// is equal to the number of closing brackets

#include

using namespace std;

 

9
0

9
1
9
2

9
3

9
4_______0_______1
9
6

9
4_______0_______1
9
9

9
4_______0_______1
OP: 9
2

9
4_______12_______4
OP: 9
5
OP: 9
6
OP: 9
7

9
4_______12_______4// C++ program to find an index k which0
OP: 9
6 // C++ program to find an index k which2

 

9
4_______808_______4

9
4// C++ program to find an index k which6

9
4_______808_______8 // C++ program to find an index k which9// decides the number of opening brackets0// decides the number of opening brackets1

// decides the number of opening brackets2// decides the number of opening brackets3

9
4_______808_______8 // decides the number of opening brackets6// decides the number of opening brackets7// decides the number of opening brackets1

// decides the number of opening brackets2// is equal to the number of closing brackets0

 

9
4_______810_______2

9
4// is equal to the number of closing brackets4

9
4_______810_______6 // is equal to the number of closing brackets7
9
1 // is equal to the number of closing brackets9

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 #include4// decides the number of opening brackets0 // decides the number of opening brackets1

#include7#include8

// decides the number of opening brackets2using0

#include7using2

9
4using4

 

9
4using6

9
4// is equal to the number of closing brackets4

9
4_______810_______6 // is equal to the number of closing brackets7
9
1 namespace3

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 #include4// decides the number of opening brackets7 // decides the number of opening brackets1

#include7std;2

// decides the number of opening brackets2using0

#include7std;6

9
4using4

 

9
4_______0_______00

9
4_______0_______02

9
4_______808_______8
9
05

// decides the number of opening brackets2

9
07
9
08

9
4_______808_______8
9
11

// decides the number of opening brackets2

9
07
9
14

 

9
4_______0_______16

9
4_______0_______18

9
4_______810_______6 // is equal to the number of closing brackets7
9
1
9
23

// decides the number of opening brackets2// C++ program to find an index k which8

9
26

#include7

9
28

 

9
4_______0_______07
9
31

using4

 

9
33

9
1
9
35

9
3

9
4_______0_______38
9
39
9
40

9
4_______0_______42

9
4_______0_______07
9
14

using4

Java




9
47

// decides the number of opening brackets

// is equal to the number of closing brackets

 

9
50
9
51
9
52

9
3

9
4_______0_______55

9
4_______0_______57
9
1
9
59

9
4_______0_______3

// decides the number of opening brackets2

9
1
9
6

// decides the number of opening brackets2

9
1
9
67_______0_______68
9
1
9
70
9
71
9
72

// decides the number of opening brackets2

9
1   
9
75_______0_______68
9
1
9
70
9
71
9
72

// decides the number of opening brackets2

9
1
9
83
9
71
9
40

9
86

// decides the number of opening brackets2

9
88
9
89
9
90
9
89
9
40

// decides the number of opening brackets2

9
94
9
89
9
40

// decides the number of opening brackets2// C++ program to find an index k which8

9
99_______0_______89
OP: 9
01// decides the number of opening brackets0// decides the number of opening brackets1

#include7

9
88
9
71
9
90
9
71
9
40

// decides the number of opening brackets2// C++ program to find an index k which8

OP: 9
12
9
71
OP: 9
14// decides the number of opening brackets7// decides the number of opening brackets1

#include7

OP: 9
18
9
71
9
90
9
71
9
40

9
86

// decides the number of opening brackets2// is equal to the number of closing brackets2

// decides the number of opening brackets2// is equal to the number of closing brackets4

// decides the number of opening brackets2// is equal to the number of closing brackets6 // is equal to the number of closing brackets7

9
1
OP: 9
32
9
71
OP: 9
34

// decides the number of opening brackets2

9
3

#include7// C++ program to find an index k which8

OP: 9
39// decides the number of opening brackets0 // decides the number of opening brackets1

OP: 9
42
OP: 9
43
9
71
OP: 9
45
9
71
9
40

#include7using0

OP: 9
42____12_______43
9
71
OP: 9
53

// decides the number of opening brackets2using4

9
86

// decides the number of opening brackets2using6

// decides the number of opening brackets2// is equal to the number of closing brackets4

// decides the number of opening brackets2// is equal to the number of closing brackets6 // is equal to the number of closing brackets7

9
1
OP: 9
65
OP: 9
66
OP: 9
67
9
89
OP: 9
69

// decides the number of opening brackets2

9
3

#include7// C++ program to find an index k which8

OP: 9
39// decides the number of opening brackets7 // decides the number of opening brackets1

OP: 9
42
OP: 9
78
9
71
OP: 9
80
9
71
9
40

#include7using0

OP: 9
42
OP: 9
78
9
71
9
72

// decides the number of opening brackets2using4

9
86

// decides the number of opening brackets2

9
00

// decides the number of opening brackets2

9
02

// decides the number of opening brackets2// C++ program to find an index k which8

OP: 9
98
9
89// decides the number of opening brackets1

#include7

9
07
9
08

// decides the number of opening brackets2// C++ program to find an index k which8 // C++ program to find an index k which06

9
89// C++ program to find an index k which08
9
89// decides the number of opening brackets1

#include7

9
07
9
89
9
40

9
4

// decides the number of opening brackets2

9
16

// decides the number of opening brackets2

9
18

// decides the number of opening brackets2// is equal to the number of closing brackets6 // is equal to the number of closing brackets7

9
1 // C++ program to find an index k which24
9
89// C++ program to find an index k which26

#include7____808_______8

9
26

OP: 9
42____0_______28

9
86

// decides the number of opening brackets2

9
07
9
31

9
4using4

9
4

9
4_______808_______40

9
4_______0_______50
9
57 // C++ program to find an index k which44 // C++ program to find an index k which45

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which49

9
39
9
40

// decides the number of opening brackets2// C++ program to find an index k which53

9
4using4

using4

Python3




// C++ program to find an index k which57

// C++ program to find an index k which58 // C++ program to find an index k which59// C++ program to find an index k which60// C++ program to find an index k which61

9
4_______808_______63_______808_______64 // C++ program to find an index k which65// is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets1

9
4_______808_______70 // C++ program to find an index k which64 // C++ program to find an index k which72
9
89// C++ program to find an index k which74// C++ program to find an index k which75 // C++ program to find an index k which76// C++ program to find an index k which77
9
71// decides the number of opening brackets1

9
4_______808_______81_______808_______64 // C++ program to find an index k which72
9
89// C++ program to find an index k which74// C++ program to find an index k which75 // C++ program to find an index k which76// C++ program to find an index k which77
9
71// decides the number of opening brackets1

9
4_______808_______92_______808_______64 // C++ program to find an index k which94
9
71

9
4

9
4_______808_______70// C++ program to find an index k which72
9
89// C++ program to find an index k which74// C++ program to find an index k which64
9
89

9
4_______809_______05// C++ program to find an index k which64
9
89

9
4_______808_______8 // is equal to the number of closing brackets7// C++ program to find an index k which60// C++ program to find an index k which72
9
89// C++ program to find an index k which74_______808_______64// C++ program to find an index k which64// decides the number of opening brackets0// C++ program to find an index k which61

// decides the number of opening brackets2// C++ program to find an index k which70// C++ program to find an index k which72

9
71// C++ program to find an index k which74// C++ program to find an index k which64
9
71

9
4_______808_______8 // is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets30// C++ program to find an index k which94
9
71// C++ program to find an index k which74// C++ program to find an index k which64// C++ program to find an index k which64 // decides the number of opening brackets7// C++ program to find an index k which61

// decides the number of opening brackets2// decides the number of opening brackets39// C++ program to find an index k which94

9
71// C++ program to find an index k which74// C++ program to find an index k which64
9
71

9
4

9
4// decides the number of opening brackets47

9
4// decides the number of opening brackets49

9
4// decides the number of opening brackets51

9
4_______810_______6 // decides the number of opening brackets54_______809_______55 // decides the number of opening brackets56// is equal to the number of closing brackets7
9
71// decides the number of opening brackets59

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets64// C++ program to find an index k which64_______808_______64 // decides the number of opening brackets0// C++ program to find an index k which61

#include7// C++ program to find an index k which70// decides the number of opening brackets71_______808_______77

9
71// C++ program to find an index k which74// C++ program to find an index k which64 // C++ program to find an index k which70// decides the number of opening brackets64// C++ program to find an index k which77
9
71

// decides the number of opening brackets2using0// decides the number of opening brackets82

#include7// C++ program to find an index k which70// decides the number of opening brackets71_______808_______77

9
71// C++ program to find an index k which74// C++ program to find an index k which64 // C++ program to find an index k which70// decides the number of opening brackets64

9
4

9
4_______809_______94

9
4_______809_______96

9
4// decides the number of opening brackets51

9
4_______810_______6 // decides the number of opening brackets54// decides the number of opening brackets55 // decides the number of opening brackets56// C++ program to find an index k which76// C++ program to find an index k which94
OP: 9
66// is equal to the number of closing brackets07// C++ program to find an index k which94
9
71// is equal to the number of closing brackets07// C++ program to find an index k which94
9
71_______808_______61

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets64// C++ program to find an index k which64// C++ program to find an index k which64 // decides the number of opening brackets7// C++ program to find an index k which61

#include7// is equal to the number of closing brackets24// C++ program to find an index k which64 // is equal to the number of closing brackets26// C++ program to find an index k which77

9
71// C++ program to find an index k which74// C++ program to find an index k which77
9
71

// decides the number of opening brackets2using0// decides the number of opening brackets82

#include7// is equal to the number of closing brackets24_______808_______64 // is equal to the number of closing brackets26// C++ program to find an index k which77

9
71// C++ program to find an index k which74

9
4

9
4// is equal to the number of closing brackets44

9
4_______810_______46

9
4_______808_______8 // is equal to the number of closing brackets7_______808_______70// is equal to the number of closing brackets51// C++ program to find an index k which64// C++ program to find an index k which64
9
89// C++ program to find an index k which61

// decides the number of opening brackets2

9
07 // C++ program to find an index k which65

9
4_______808_______8 // C++ program to find an index k which06
9
89// C++ program to find an index k which74// C++ program to find an index k which64// C++ program to find an index k which64
9
89// C++ program to find an index k which61

// decides the number of opening brackets2

9
07
9
89

9
4

9
4// is equal to the number of closing brackets73

9
4// is equal to the number of closing brackets75

9
4// is equal to the number of closing brackets77

9
4_______810_______6 // decides the number of opening brackets54_______809_______55 // decides the number of opening brackets56// C++ program to find an index k which76// C++ program to find an index k which77
9
71// C++ program to find an index k which61

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7// C++ program to find an index k which70// decides the number of opening brackets64// C++ program to find an index k which64// C++ program to find an index k which64 // is equal to the number of closing brackets94

#include7// C++ program to find an index k which92// C++ program to find an index k which64 // decides the number of opening brackets54

9
4

9
4_______0_______07 // C++ program to find an index k which92

9
4

#include04

// C++ program to find an index k which60 // C++ program to find an index k which64

9
39

#include08#include09// C++ program to find an index k which60#include11

 

#include12

#include13

C#




#include14

#include15

#include16

#include17

________ 812 _______ ________ 811 _______ 19

 

9
51
9
52

9
3

9
55

9
57
9
1 // C++ program to find an index k which59#include27 #include28

9
3

9
4_______0_______1 #include32

9
4_______0_______1#include35_______0_______68
9
1#include38

9
4_______0_______1#include41_______0_______68
9
1#include38

9
4_______0_______1
OP: 9
2

 

9
4_______808_______4

9
4// C++ program to find an index k which6

9
4_______808_______8 #include54// decides the number of opening brackets0// decides the number of opening brackets1

// decides the number of opening brackets2// decides the number of opening brackets3

9
4_______808_______8 #include61// decides the number of opening brackets7// decides the number of opening brackets1

// decides the number of opening brackets2____811_______65

 

9
4#include67

9
4#include69

9
4// is equal to the number of closing brackets4

9
4_______810_______6 // is equal to the number of closing brackets7
9
1 // is equal to the number of closing brackets9

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 #include81// decides the number of opening brackets0// decides the number of opening brackets1

#include7#include85

// decides the number of opening brackets2using0

#include7#include89

9
4using4

 

9
4#include93

9
4#include95

9
4// is equal to the number of closing brackets4

9
4_______810_______6 // is equal to the number of closing brackets7
9
1 using02

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 #include81// decides the number of opening brackets7// decides the number of opening brackets1

#include7using11

// decides the number of opening brackets2using0

#include7using15

9
4using4

 

9
4using19

9
4using21

9
4_______0_______02

9
4_______808_______8
9
05

// decides the number of opening brackets2

9
07
9
08

9
4_______808_______8
9
11

// decides the number of opening brackets2

9
07
9
14

 

9
4using37

9
4using39

9
4using41

9
4_______810_______6 // is equal to the number of closing brackets7
9
1 using46

// decides the number of opening brackets2// C++ program to find an index k which8

9
26

#include7

9
28

 

9
4_______0_______07
9
31

using4

 

using56

9
50
9
57 // C++ program to find an index k which44 using60

9
3

9
4_______811_______27 using64
9
39
9
40

9
4using68

using4

using4

 

using71

using72

PHP




using73

9
55

using75 // C++ program to find an index k which59using77// decides the number of opening brackets1

9
3

9
4_______812_______81 // C++ program to find an index k which64using83// is equal to the number of closing brackets7using77using86

9
4_______812_______88 // C++ program to find an index k which64using90using91using81 using93

9
4_______812_______95 // C++ program to find an index k which64using90using91using81 using93

9
4_______813_______02 namespace03

9
4

9
4_______812_______88namespace07

9
4_______812_______95____808_______72using81namespace12

9
4_______808_______8 // is equal to the number of closing brackets7using77namespace17// decides the number of opening brackets0// decides the number of opening brackets1

9
4_______812_______88namespace22

9
4_______808_______8 // is equal to the number of closing brackets7using77// C++ program to find an index k which72using81 namespace29// decides the number of opening brackets7// decides the number of opening brackets1

9
4_______812_______95// C++ program to find an index k which72using81 namespace36

9
4

9
4#include93

9
4namespace41

9
4// is equal to the number of closing brackets4

9
4_______810_______6 // is equal to the number of closing brackets7namespace47 namespace48namespace47 namespace50using81
9
40namespace47namespace54

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7using77// C++ program to find an index k which72namespace47// C++ program to find an index k which08// decides the number of opening brackets0// decides the number of opening brackets1

#include7using88// C++ program to find an index k which72namespace47 namespace70using88// C++ program to find an index k which72namespace47namespace74

// decides the number of opening brackets2using0

#include7using88// C++ program to find an index k which72namespace47 namespace70using88// C++ program to find an index k which72namespace47

9
72

9
4using4

9
4

9
4#include93

9
4#include95

9
4// is equal to the number of closing brackets4

9
4_______810_______6 // is equal to the number of closing brackets7namespace47 // C++ program to find an index k which64using81 std;01namespace47 std;03namespace47std;05

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7using77// C++ program to find an index k which72namespace47// C++ program to find an index k which08// decides the number of opening brackets7// decides the number of opening brackets1

// decides the number of opening brackets2using95// C++ program to find an index k which72namespace47

9
90using95// C++ program to find an index k which72namespace47 std;25

// decides the number of opening brackets2using0

// decides the number of opening brackets2using95// C++ program to find an index k which72namespace47

9
90using95// C++ program to find an index k which72namespace47 std;36

9
4using4

9
4

9
4using19

9
4using21

9
4_______0_______02

9
4_______808_______8 // is equal to the number of closing brackets7using88// C++ program to find an index k which72using81std;52

// decides the number of opening brackets2

9
07 using81
9
40

9
4_______808_______8 // is equal to the number of closing brackets7using95std;61

// decides the number of opening brackets2

9
07
9
14

9
4

9
4using37

9
4using39

9
4using41

9
4_______810_______6 // is equal to the number of closing brackets7namespace47 std;76namespace47 std;78using81
9
40namespace47namespace54

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7using88_______808_______72namespace47// C++ program to find an index k which08using95// C++ program to find an index k which72namespace47std;93

#include7namespace02 // C++ program to find an index k which64namespace47

9
40

9
4

9
4_______0_______07 namespace02
9
40

using4

9
4

using56

using77 // C++ program to find an index k which64

9
39
9
40

9
011 #include09using77
9
014

 

using71

using72

9
017

Javascript




9
018

 

9
019

// decides the number of opening brackets

// is equal to the number of closing brackets

9
4

9
55

using75

9
025

9
3

9
4_______0_______028

9
4_______0_______030
9
68
9
032

9
4_______0_______034
9
68
9
032

9
4_______810_______6
9
039

9
4_______0_______3

// decides the number of opening brackets2

9
043

// decides the number of opening brackets2

9
045

9
4using4

9
4_______0_______049

9
050

9
4_______808_______4

9
4// C++ program to find an index k which6

9
4

9
4_______808_______8 #include54// decides the number of opening brackets0// decides the number of opening brackets1

// decides the number of opening brackets2// decides the number of opening brackets3

9
4_______808_______8 #include61// decides the number of opening brackets7// decides the number of opening brackets1

// decides the number of opening brackets2____811_______65

9
050

9
4_______810_______2

9
4// is equal to the number of closing brackets4

9
4_______810_______6
9
077

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 #include81// decides the number of opening brackets0// decides the number of opening brackets1

#include7#include85

// decides the number of opening brackets2using0

#include7#include89

9
4using4

9
050

9
4using6

9
4// is equal to the number of closing brackets4

9
4_______810_______6
9
100

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which8 #include81// decides the number of opening brackets7// decides the number of opening brackets1

#include7using11

// decides the number of opening brackets2using0

#include7using15

9
4using4

9
050

9
4_______0_______118

9
4_______0_______02

9
4_______808_______8
9
05

// decides the number of opening brackets2

9
07
9
08

9
4_______808_______8
9
11

// decides the number of opening brackets2

9
07
9
14

9
133

9
4_______0_______135

9
4_______0_______18

9
4_______810_______6
9
140

// decides the number of opening brackets2// C++ program to find an index k which8

9
26

#include7

9
28

9
050

9
4_______0_______07
9
31

using4

 

using56

9
152_______0_______39
9
40

 

9
155

 

9
156

 

9
157

Đầu ra

9

Thời gian phức tạp. O(N), trong đó N là kích thước của chuỗi đã cho
Không gian phụ trợ. TRÊN)

Cách tiếp cận 2.  

  • Đếm tổng số dấu ngoặc đóng trong chuỗi và lưu trữ trong một biến, giả sử cnt_close
  • Vì vậy, số lượng dấu ngoặc mở là độ dài của (chuỗi - số lượng) của dấu ngoặc đóng
  • Duyệt lại chuỗi nhưng bây giờ giữ số lượng dấu ngoặc mở trong chuỗi, giả sử cnt_open
  • Bây giờ trong khi di chuyển ngang, hãy đặt chỉ mục là i, vì vậy số lượng dấu ngoặc đóng cho đến chỉ mục đó sẽ là (i+1 – cnt_open)
  • Do đó, chúng ta có thể kiểm tra chỉ mục nào, số lượng dấu ngoặc mở trong phần đầu tiên bằng với số lượng dấu ngoặc đóng trong phần thứ hai
  • Phương trình trở thành cnt_close – (i+1 – cnt_open) = cnt_open, chúng ta phải tìm i
  • Sau khi đánh giá phương trình trên, chúng ta có thể thấy cnt_open bị hủy ở cả hai phía nên không cần

Thực hiện

C++




// C++ program to find an index k which

// decides the number of opening brackets

// is equal to the number of closing brackets

9
161

using namespace std;

 

9
0

9
1
9
2

9
3

9
4_______0_______170

9
4_______0_______1
9
173// decides the number of opening brackets7using86

9
4_______810_______6 // is equal to the number of closing brackets7
9
1
9
180
9
181
9
182

// decides the number of opening brackets2____808_______8

9
185

#include7

9
07
9
188

9
4_______0_______190

9
4_______0_______07
9
193

using4

 

9
33

9
1
9
35

9
3

9
4_______0_______38
9
39
9
40

9
4_______0_______42

9
4_______0_______07
9
14

using4

 

9
209

Java




9
47

// decides the number of opening brackets

// is equal to the number of closing brackets

 

9
50
9
51
9
215

9
4_______0_______55

9
4_______0_______57
9
1
9
59

9
4_______0_______3

// decides the number of opening brackets2

9
1
9
6

// decides the number of opening brackets2

9
1
9
229
9
89
9
40

// decides the number of opening brackets2// is equal to the number of closing brackets6 // is equal to the number of closing brackets7

9
1
OP: 9
32
9
89
OP: 9
34

#include7// C++ program to find an index k which8

9
241// decides the number of opening brackets7// decides the number of opening brackets1

OP: 9
42____0_______245

// decides the number of opening brackets2// is equal to the number of closing brackets6 // is equal to the number of closing brackets7

9
1
OP: 9
32
9
89
OP: 9
34

#include7// C++ program to find an index k which8

9
185

OP: 9
42____0_______07
9
188

// decides the number of opening brackets2

9
190

// decides the number of opening brackets2

9
07
9
08

9
4using4

 

9
4_______808_______40

9
4_______0_______50
9
57 // C++ program to find an index k which44 // C++ program to find an index k which45

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which49

9
39
9
40

// decides the number of opening brackets2// C++ program to find an index k which53

9
4using4

using4

 

9
284

Python3




// C++ program to find an index k which57

// C++ program to find an index k which58 // C++ program to find an index k which59// C++ program to find an index k which60// C++ program to find an index k which61

9
4_______0_______291// C++ program to find an index k which64
9
89

9
4_______808_______63_______808_______64 // C++ program to find an index k which65// is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets1

9
4_______810_______6 // decides the number of opening brackets54_______809_______55 // decides the number of opening brackets56// is equal to the number of closing brackets7
9
89// decides the number of opening brackets59

// decides the number of opening brackets2// C++ program to find an index k which8// is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets64// C++ program to find an index k which64// C++ program to find an index k which64 // decides the number of opening brackets7// C++ program to find an index k which61

#include7

9
291// C++ program to find an index k which64
9
291// C++ program to find an index k which77
9
71

 

9
4_______810_______6 // decides the number of opening brackets54_______809_______55 // decides the number of opening brackets56// is equal to the number of closing brackets7
9
89// decides the number of opening brackets59

// decides the number of opening brackets2// C++ program to find an index k which8

9
334// C++ program to find an index k which64// C++ program to find an index k which64
9
337

#include7

9
07 // decides the number of opening brackets54

9
4_______0_______342

9
4_______0_______07 // C++ program to find an index k which63

 

 

#include04

// C++ program to find an index k which60 // C++ program to find an index k which64

9
39

#include08#include09// C++ program to find an index k which60#include11

 

9
354

C#




________ 812 _______ ________ 811 _______ 19

 

9
357

// decides the number of opening brackets

// is equal to the number of closing brackets

9
50
9
51
9
52

9
3

 

9
133
9
55

9
133
9
50
9
57
9
1
9
59

9
133
9
3

9
4_______0_______374 #include32

9
4_______0_______374
9
378

9
4_______810_______6 // is equal to the number of closing brackets7
9
1
9
383

9
384_______808_______8 #include81// decides the number of opening brackets7
9
388

// decides the number of opening brackets2

9
245

9
384____812_______4

9
4using4

9
4_______810_______6 // is equal to the number of closing brackets7
9
1
9
383

9
384_______808_______8
9
402

// decides the number of opening brackets2____0_______07

9
188

9
384____812_______4

9
4using4

 

9
4_______0_______190

9
4_______0_______07
9
08

9
133____812_______4

 

9
133// C++ program to find an index k which40

9
133
9
50
9
57 // C++ program to find an index k which44
9
423

9
133
9
3

9
4_______0_______374 using64
9
39
9
40

9
4_______0_______432

9
133____812_______4

using4

 

______________436

Javascript




9
018

 

9
019

// decides the number of opening brackets

// is equal to the number of closing brackets

9
4

9
55

using75

9
025

9
3

9
4_______0_______028

9
4_______0_______449

9
4_______810_______6
9
452

// decides the number of opening brackets2// C++ program to find an index k which8 #include81// decides the number of opening brackets7// decides the number of opening brackets1

#include7

9
245

9
460

9
86// is equal to the number of closing brackets6
9
452

9
460_______808_______8
9
185

#include7

9
07
9
188

// decides the number of opening brackets2

9
190

// decides the number of opening brackets2

9
07
9
08

9
4

using56

9
152_______0_______39
9
40

 

9
155

 

9
284

 

9
157

PHP




using73

9
55

using75 // C++ program to find an index k which59using77// decides the number of opening brackets1

9
3

9
4_______812_______81 // C++ program to find an index k which64using83// is equal to the number of closing brackets7using77using86

9
4_______0_______498 std;76

9
384_______810_______6 // is equal to the number of closing brackets7namespace47 namespace48namespace47 namespace50_______812_______81
9
40namespace47namespace54

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7using77// C++ program to find an index k which72namespace47// C++ program to find an index k which08// decides the number of opening brackets7// decides the number of opening brackets1

#include7

9
498
9
522

9
384

9
384_______810_______6 // is equal to the number of closing brackets7namespace47 namespace48namespace47 namespace50_______812_______81
9
40namespace47namespace54

// decides the number of opening brackets2// C++ program to find an index k which8 // is equal to the number of closing brackets7

9
498
9
539namespace47// decides the number of opening brackets1

9
542_______0_______07 namespace47
9
40

9
384______0_______07 using81
9
40

using4

9
4

using56

using77 // C++ program to find an index k which64

9
39
9
40

9
011 #include09using77
9
014

 

9
284

9
017

Đầu ra

9

Thời gian phức tạp. O(N), Trong đó N là kích thước của chuỗi đã cho
Không gian phụ trợ. Ô(1)

Cách tiếp cận 3. (Giải pháp trong 1 lần lặp)

  • Đếm tổng số dấu ngoặc đóng trong chuỗi và lưu trữ trong biến, giả sử cnt_close
  • Vì vậy, số lượng dấu ngoặc mở là độ dài của (chuỗi - số lượng) của dấu ngoặc đóng
  • Duyệt lại chuỗi nhưng bây giờ giữ số lượng dấu ngoặc mở trong chuỗi, giả sử cnt_open
  • Bây giờ trong khi di chuyển ngang, hãy đặt chỉ mục là i, vì vậy số lượng dấu ngoặc đóng cho đến chỉ mục đó sẽ là (i+1 – cnt_open)
  • Do đó, chúng ta có thể kiểm tra chỉ mục nào, số lượng dấu ngoặc mở trong phần đầu tiên bằng với số lượng dấu ngoặc đóng trong phần thứ hai
  • Phương trình trở thành cnt_close – (i+1 – cnt_open) = cnt_open, chúng ta phải tìm i
  • Sau khi đánh giá phương trình trên, chúng ta có thể thấy cnt_open bị hủy ở cả hai phía nên không cần vòng lặp bổ sung và chỉ cần trả về cnt_close

C++




9
563

#include

using namespace std;

 

9
55

9
1
9
570

9
4_______0_______1
9
378

9
4_______0_______1
9
576

9
4_______810_______6// is equal to the number of closing brackets7
9
1
9
581

// decides the number of opening brackets2// C++ program to find an index k which8#include81// decides the number of opening brackets7// decides the number of opening brackets1

#include7

9
245

9
4using4

9
4_______0_______07
9
593

using4

 

9
1
9
596

9
4_______0_______38
9
39
9
40

9
4_______0_______602

9
4_______0_______07
9
14

using4

 

9
607

Java




9
608

9
609
9
610

 

9
50
9
51
9
215

9
4_______0_______55

9
4_______0_______57
9
1
9
59

9
4_______0_______3

// decides the number of opening brackets2

9
1
9
229
9
89
9
40

// decides the number of opening brackets2

9
1
9
576

// decides the number of opening brackets2// is equal to the number of closing brackets6 // is equal to the number of closing brackets7

9
1
OP: 9
32
9
89
9
636

#include7// C++ program to find an index k which8

9
241// decides the number of opening brackets7// decides the number of opening brackets1

OP: 9
42____0_______245

// decides the number of opening brackets2using4

// decides the number of opening brackets2

9
07
9
593

9
4using4

9
4_______0_______50
9
57 // C++ program to find an index k which44 // C++ program to find an index k which45

9
4_______0_______3

// decides the number of opening brackets2// C++ program to find an index k which49

9
39
9
40

// decides the number of opening brackets2

9
663
9
664
9
665

9
4using4

using4

 

9
669

Python3




// C++ program to find an index k which57

// C++ program to find an index k which58 // C++ program to find an index k which59// C++ program to find an index k which60// C++ program to find an index k which61

9
4_______0_______291// C++ program to find an index k which64
9
89

9
4_______808_______63_______808_______64 // C++ program to find an index k which65// is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets1

9
4_______810_______6 // decides the number of opening brackets54_______809_______55 // decides the number of opening brackets56// is equal to the number of closing brackets7
9
89// decides the number of opening brackets59

// decides the number of opening brackets2// C++ program to find an index k which8// is equal to the number of closing brackets7// C++ program to find an index k which60// decides the number of opening brackets64// C++ program to find an index k which64// C++ program to find an index k which64 // decides the number of opening brackets7// C++ program to find an index k which61

#include7

9
291// C++ program to find an index k which64
9
291// C++ program to find an index k which77
9
71

 

9
4_______0_______07
9
291

 

#include04

// C++ program to find an index k which60 // C++ program to find an index k which64

9
39

#include08

9
717_______0_______718// decides the number of opening brackets1

______________720

Javascript




______________721

 

9
55

using75

9
724

9
4_______0_______726

9
4_______0_______728

9
4_______810_______6
9
731

// decides the number of opening brackets2// C++ program to find an index k which8#include81// decides the number of opening brackets7// decides the number of opening brackets1

#include7

9
245

9
4using4

9
4_______0_______07
9
593

using4

 

9
4_______0_______152
9
39
9
40

9
4_______0_______155

9
050

Đầu ra

OP: 9

Thời gian phức tạp. O(N), Trong đó N là kích thước của chuỗi đã cho
Không gian phụ trợ. Ô(1)

Bài viết này được đóng góp bởi Sahil Chhabra (akku)  và Sankararaman K. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác.  

Bạn có thể biến một mảng thành một chuỗi JavaScript không?

Để chuyển đổi một mảng thành một chuỗi trong Javascript, chúng ta chỉ cần áp dụng phương thức toString() trên mảng đã cho và chúng ta . Đầu tiên, javascript nội bộ chuyển đổi từng phần tử thành chuỗi và sau đó cụ thể hóa chúng để trả về chuỗi cuối cùng.

Làm cách nào để thêm dấu ngoặc vào chuỗi trong JavaScript?

Trong JavaScript, phép nội suy chuỗi được thực hiện bằng chữ mẫu. Các ký tự mẫu này sử dụng dấu gạch ngược (“) để bọc một chuỗi bên trong và ký hiệu $ với một cặp dấu ngoặc nhọn {} được sử dụng để nhúng biểu thức trong một ký tự mẫu để nội suy chuỗi.

Bạn có thể sử dụng các phương thức mảng trên các chuỗi trong JS không?

Bởi vì theo nhiều cách, các chuỗi hoạt động như thể chúng là các mảng ký tự, nhiều phương thức mảng tiêu chuẩn của JavaScript cũng có thể hoạt động trên các chuỗi . Tuy nhiên, nhiều không phải là tất cả.

Làm cách nào để in mảng không có dấu ngoặc JavaScript?

Để chuyển đổi một mảng thành một chuỗi trong JavaScript, bạn có thể sử dụng phương thức toString(). Phương thức toString() trả về các phần tử của mảng dưới dạng một chuỗi không có dấu ngoặc vuông [ và ]