-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathMinimumCostToMakeStringValid.cpp
78 lines (67 loc) · 1.68 KB
/
MinimumCostToMakeStringValid.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// https://www.codingninjas.com/codestudio/problems/minimum-cost-to-make-string-valid_1115770?leftPanelTab=0
#include <bits/stdc++.h>
int findMinimumCost(string str) {
// Write your code here
if((str.length() % 2) != 0) return -1;
stack<char> ch;
int cost=0;
for(int i=0; i<str.length(); i++) {
if(str[i] == '{') {
ch.push('{');
}else {
if(ch.empty()) {
cost++;
ch.push('{');
}else {
ch.pop();
}
}
}
cost += ch.size()/2;
return cost;
// if(s.length()%2!=0)
// return -1;
// stack<char> st;
// for(int i=0;i<s.length();i++){
// if(st.empty()){
// st.push(s[i]);
// continue;
// }
// char t= st.top();
// if(t=='{' && s[i]=='}')
// st.pop();
// else
// st.push(s[i]);
// }
// int count=0;
// if(st.empty())
// return 0;
// else{
// char ch=st.top();
// st.pop();
// while(!st.empty()){
// if(st.size()%2!=0){
// if(ch=='}' && st.top()=='}')
// count++;
// else if(ch=='{' && st.top()=='{')
// count++;
// else
// count+=2;
// }
// else{
// ch=st.top();
// }
// st.pop();
// }
// }
// return count;
// // int a=0, b=0;
// // while(!st.empty()){
// // if(st.top()=='{')
// // a++;
// // else
// // b++;
// // st.pop();
// // }
// // return ((a+1)/2)+((b+1)/2);
}