Skip to content

Commit c3a69ac

Browse files
jbhattacjbhattac
authored andcommitted
new algoriths added
1 parent 4040091 commit c3a69ac

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

ExpressionBalance.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ds.algo.solutions;
2+
3+
import java.util.Stack;
4+
5+
public class ExpressionBalance {
6+
7+
public static void main(String[] args) {
8+
System.out.println(isBalanced("{{{([[[]]])()()()}}}"));
9+
System.out.println(isBalanced("{{{([[[m+l]]])(x+3)(p-q-w-e-e-e)(f-d-r-e)}}}"));
10+
System.out.println(isBalanced("{{{([[[m+l]]])(x+3)(p-q-w-e-e-e)(f-d-r-e)}}}{]"));
11+
}
12+
13+
static String isBalanced(String s) {
14+
// Complete this function
15+
int b1 = 0, b2 = 0, b3 = 0;
16+
char ch;
17+
Stack<Character> nextClose = new Stack<>();
18+
19+
for (int i = 0; i < s.length(); i++){
20+
ch = s.charAt(i);
21+
if (ch == '[') {
22+
b3++;
23+
nextClose.push(']');
24+
}
25+
else if (ch == ']') {
26+
b3--;
27+
if (nextClose.isEmpty() || nextClose.pop() != ch) return "NO";
28+
if(b3<0) {
29+
return "NO";
30+
}
31+
}
32+
else if (ch == '{') {
33+
b2++;
34+
nextClose.push('}');
35+
}
36+
else if (ch == '}') {
37+
b2--;
38+
if (nextClose.isEmpty() || nextClose.pop() != ch) return "NO";
39+
if(b2<0) {
40+
return "NO";
41+
}
42+
}
43+
else if (ch == '(') {
44+
b1++;
45+
nextClose.push(')');
46+
}
47+
else if (ch == ')') {
48+
b1--;
49+
if (nextClose.isEmpty() || nextClose.pop() != ch) return "NO";
50+
if(b1<0) {
51+
return "NO";
52+
}
53+
}
54+
}
55+
return b1 + b2 + b3 == 0 ? "YES" : "NO";
56+
}
57+
}

RomanConverter.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package ds.algo.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class RomanConverter {
7+
8+
public static Map<Integer, String> INTEGER_TO_ROMAN_MAP = new HashMap<Integer, String>() {
9+
{
10+
put(1, "I");
11+
put(4, "IV");
12+
put(5, "V");
13+
put(9, "IX");
14+
put(10, "X");
15+
put(40, "XL");
16+
put(50, "L");
17+
put(90, "XC");
18+
put(100, "C");
19+
put(400, "CD");
20+
put(500, "D");
21+
put(900, "CM");
22+
put(1000, "M");
23+
}
24+
};
25+
26+
public static Map<String, Integer> ROMAN_TO_INTEGER = new HashMap<String, Integer>() {
27+
{
28+
put("I", 1);
29+
put("V", 5);
30+
put("X", 10);
31+
put("L", 50);
32+
put("C", 100);
33+
put("D", 500);
34+
put("M", 1000);
35+
}
36+
};
37+
38+
private static Integer[] BASES = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
39+
40+
public static void main(String[] args) {
41+
// System.out.println(toRoman(666));
42+
System.out.println(toInteger("DCLXVI"));
43+
}
44+
45+
public static String toRoman(int num) {
46+
47+
StringBuilder result = new StringBuilder();
48+
49+
for (int i : BASES) {
50+
while (num >= i) {
51+
result.append(INTEGER_TO_ROMAN_MAP.get(i));
52+
num = num - i;
53+
}
54+
}
55+
56+
return result.toString();
57+
}
58+
59+
public static Integer toInteger(String str) {
60+
// Initialize result
61+
Integer res = 0;
62+
63+
for (int i = 0; i < str.length(); i++) {
64+
// Getting value of symbol s[i]
65+
Integer s1 = ROMAN_TO_INTEGER.get(String.valueOf(str.charAt(i)));
66+
67+
// Getting value of symbol s[i+1]
68+
if (i + 1 < str.length()) {
69+
Integer s2 = ROMAN_TO_INTEGER.get(String.valueOf(str.charAt(i + 1)));
70+
71+
// Comparing both values
72+
if (s1 >= s2) {
73+
// Value of current symbol is greater
74+
// or equalto the next symbol
75+
res = res + s1;
76+
} else {
77+
res = res + s2 - s1;
78+
i++; // Value of current symbol is
79+
// less than the next symbol
80+
}
81+
} else {
82+
res = res + s1;
83+
i++;
84+
}
85+
}
86+
87+
return res;
88+
}
89+
}

0 commit comments

Comments
 (0)