Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit d10124d

Browse files
author
mo.elmedany@gmail.com
committed
more algorithms
1 parent c348904 commit d10124d

12 files changed

+476
-6
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ hs_err_pid*
2626
*.classpath
2727
*.project
2828
.settings/
29+
/target/

Diff for: pom.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>Algorithms</groupId>
6+
<artifactId>Algorithms</artifactId>
7+
<version>1.0</version>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>junit</groupId>
12+
<artifactId>junit</artifactId>
13+
<version>4.12</version>
14+
<scope>test</scope>
15+
</dependency>
16+
</dependencies>
17+
18+
<build>
19+
<sourceDirectory>src</sourceDirectory>
20+
<plugins>
21+
<plugin>
22+
<artifactId>maven-compiler-plugin</artifactId>
23+
<version>3.7.0</version>
24+
<configuration>
25+
<release>9</release>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>

Diff for: src/main/java/ArrayLeftRotation.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
public class ArrayLeftRotation {
7+
8+
// public int[] solve(int rotations, int[] array) {
9+
//
10+
// while (rotations > 0) {
11+
// for (int i = 0; i < array.length - 1; i++) {
12+
// int temp = array[i];
13+
// array[i] = array[i + 1];
14+
// array[i + 1] = temp;
15+
// }
16+
// rotations--;
17+
// }
18+
//
19+
// return array;
20+
// }
21+
22+
public int[] solve(int rotations, int[] array) {
23+
24+
int[] rotated = new int[array.length];
25+
26+
for (int i = 0; i < rotated.length; i++) {
27+
int index = i - rotations < 0 ? array.length + (i - rotations) : i - rotations;
28+
rotated[index] = array[i];
29+
}
30+
31+
return rotated;
32+
}
33+
34+
}

Diff for: src/main/java/BalancedBrackets.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* @author medany
5+
*/
6+
7+
public class BalancedBrackets {
8+
9+
public String solve(String expression) {
10+
11+
Stack<String> brackets = new Stack<String>();
12+
13+
String current = brackets.pop();
14+
while (!brackets.isEmpty()) {
15+
switch (current) {
16+
case "{":
17+
case "[":
18+
case "(":
19+
brackets.push(current);
20+
break;
21+
22+
case "}":
23+
case "]":
24+
case ")":
25+
if (!brackets.isEmpty()) {
26+
String last = brackets.pop();
27+
if ((current.equals("}") && !last.equals("{")) || (current.equals("]") && !last.equals("["))
28+
|| (current.equals(")") && !last.equals("(")))
29+
return "No";
30+
}
31+
}
32+
}
33+
34+
return "Yes";
35+
}
36+
}

Diff for: src/FindEarliestDigitalTime.java renamed to src/main/java/FindEarliestDigitalTime.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@
77

88
public class FindEarliestDigitalTime {
99

10-
public static void main(String[] args) {
10+
public String solve(String s) {
1111

12-
Set<String> permutations = generatePermutations("123456"); // 12:34:56
13-
// Set<String> permutations = generatePermutations("000789"); // 07:08:09
12+
Set<String> permutations = generatePermutations(s);
1413

1514
String hours = "", minutes = "", seconds = "";
15+
1616
for (String perm : permutations) {
1717
hours = "" + perm.charAt(0) + perm.charAt(1);
1818
minutes = "" + perm.charAt(2) + perm.charAt(3);
1919
seconds = "" + perm.charAt(4) + perm.charAt(5);
2020

2121
if (Integer.parseInt(hours) <= 12 && Integer.parseInt(minutes) <= 60 && Integer.parseInt(seconds) <= 60) {
22-
System.out.println(hours + ":" + minutes + ":" + seconds);
23-
return;
22+
return hours + ":" + minutes + ":" + seconds;
2423
}
2524
}
2625

27-
System.out.println("NOT POSSIBLE");
26+
return "NOT POSSIBLE";
2827

2928
}
3029

Diff for: src/main/java/MakingAnagrams.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
/*
7+
* Alice is taking a cryptography class and finding anagrams to be very useful.
8+
* We consider two strings to be anagrams of each other if the first string's
9+
* letters can be rearranged to form the second string. In other words, both
10+
* strings must contain the same exact letters in the same exact frequency For
11+
* example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
12+
*
13+
* Alice decides on an encryption scheme involving two large strings where
14+
* encryption is dependent on the minimum number of character deletions required
15+
* to make the two strings anagrams. Can you help her find this number?
16+
*
17+
* Given two strings, and , that may or may not be of the same length, determine
18+
* the minimum number of character deletions required to make and anagrams. Any
19+
* characters can be deleted from either of the strings.
20+
*/
21+
22+
public class MakingAnagrams {
23+
24+
public int solve(String first, String second) {
25+
26+
if (first.length() != second.length())
27+
return 0;
28+
29+
int required = 0;
30+
31+
int[] letters = new int[26];
32+
33+
for (char f : first.toCharArray()) {
34+
letters[f - 'a']++;
35+
}
36+
37+
for (char s : second.toCharArray()) {
38+
letters[s - 'a']--;
39+
}
40+
41+
for (int l : letters) {
42+
required += Math.abs(l);
43+
}
44+
45+
return required;
46+
47+
}
48+
}

Diff for: src/main/java/RansomNote.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
/*
9+
* A kidnapper wrote a ransom note but is worried it will be traced back to him.
10+
* He found a magazine and wants to know if he can cut out whole words from it
11+
* and use them to create an untraceable replica of his ransom note. The words
12+
* in his note are case-sensitive and he must use whole words available in the
13+
* magazine, meaning he cannot use substrings or concatenation to create the
14+
* words he needs.
15+
*
16+
* Given the words in the magazine and the words in the ransom note, print Yes
17+
* if he can replicate his ransom note exactly using whole words from the
18+
* magazine; otherwise, print No.
19+
*/
20+
public class RansomNote {
21+
22+
public String solve(String msg, String note) {
23+
24+
Map<String, Integer> msgMap = new HashMap<String, Integer>();
25+
26+
for (String m : msg.split(" ")) {
27+
if (msgMap.containsKey(m))
28+
msgMap.put(m, msgMap.get(m) + 1);
29+
else
30+
msgMap.put(m, 1);
31+
}
32+
33+
for (String n : note.split(" ")) {
34+
if (msgMap.containsKey(n) && msgMap.get(n) > 0)
35+
msgMap.put(n, msgMap.get(n) - 1);
36+
else
37+
return "No";
38+
}
39+
40+
return "Yes";
41+
}
42+
}

Diff for: test/main/java/ArrayLeftRotationTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class ArrayLeftRotationTest {
9+
10+
@Test
11+
public void Test_1() {
12+
13+
ArrayLeftRotation alg = new ArrayLeftRotation();
14+
int rotations = 2;
15+
int[] array = new int[] { 1, 2, 3, 4, 5 };
16+
17+
int[] actual = alg.solve(rotations, array), expected = new int[] { 3, 4, 5, 1, 2 };
18+
19+
Assert.assertArrayEquals(expected, actual);
20+
}
21+
22+
@Test
23+
public void Test_2() {
24+
25+
ArrayLeftRotation alg = new ArrayLeftRotation();
26+
int rotations = 5;
27+
int[] array = new int[] { 1, 2, 3, 4, 5 };
28+
29+
int[] actual = alg.solve(rotations, array), expected = new int[] { 1, 2, 3, 4, 5 };
30+
31+
Assert.assertArrayEquals(expected, actual);
32+
}
33+
34+
}

Diff for: test/main/java/BalancedBracketsTest.java

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class BalancedBracketsTest {
9+
10+
@Test
11+
public void Test_YES() {
12+
13+
BalancedBrackets alg = new BalancedBrackets();
14+
15+
String expression = "{[()]}";
16+
17+
String actual = alg.solve(expression), expected = "Yes";
18+
19+
Assert.assertEquals(expected, actual);
20+
21+
}
22+
23+
@Test
24+
public void Test_NO() {
25+
26+
BalancedBrackets alg = new BalancedBrackets();
27+
28+
String expression = "{[()]}";
29+
30+
String actual = alg.solve(expression), expected = "{[(])}";
31+
32+
Assert.assertEquals(expected, actual);
33+
34+
}
35+
36+
@Test
37+
public void Test_YES_2() {
38+
39+
BalancedBrackets alg = new BalancedBrackets();
40+
41+
String expression = "{{[[(())]]}}";
42+
43+
String actual = alg.solve(expression), expected = "Yes";
44+
45+
Assert.assertEquals(expected, actual);
46+
47+
}
48+
49+
@Test
50+
public void Test_Bulk() {
51+
52+
BalancedBrackets alg = new BalancedBrackets();
53+
54+
String[] expression = new String[] {
55+
"[()][{}()][](){}([{}(())([[{}]])][])[]([][])(){}{{}{[](){}}}()[]({})[{}{{}([{}][])}]",
56+
"[()][{}[{}[{}]]][]{}[]{}[]{{}({}(){({{}{}[([[]][[]])()]})({}{{}})})}",
57+
"(])[{{{][)[)])(]){(}))[{(})][[{)(}){[(]})[[{}(])}({)(}[[()}{}}]{}{}}()}{({}](]{{[}}(([{]",
58+
"){[]()})}}]{}[}}})}{]{](]](()][{))])(}]}))(}[}{{)}{[[}[]", "}(]}){",
59+
"((]()(]([({]}({[)){}}[}({[{])(]{()[]}}{)}}]]{({)[}{(", "{}{({{}})}[][{{}}]{}{}(){{}[]}{}([[][{}]]())",
60+
"(){}[()[][]]{}(())()[[([])][()]{}{}(({}[]()))()[()[{()}]][]]",
61+
"()([]({}[]){}){}{()}[]{}[]()(()([[]]()))()()()[]()(){{}}()({[{}][]}[[{{}({({({})})})}]])",
62+
"[]([{][][)(])}()([}[}(})}])}))]](}{}})[]({{}}))[])(}}[[{]{}]()[(][])}({]{}[[))[[}[}{(]})()){{(]]){][",
63+
"{()({}[[{}]]()(){[{{}{[[{}]{}((({[]}{}()[])))]((()()))}(()[[[]]])((()[[](({([])()}))[]]))}]})}",
64+
"()(){{}}[()()]{}{}", "{}()([[]])({}){({[][[][[()]]{{}[[]()]}]})}[](())((())[{{}}])",
65+
"{}(((){}){[]{{()()}}()})[]{{()}{(){()(){}}}}{()}({()(()({}{}()((()((([])){[][{()}{}]})))))})",
66+
"][[{)())))}[)}}}}[{){}()]([][]){{{{{[)}]]{([{)()][({}[){]({{", "{{}(",
67+
"{[{((({}{({({()})()})[]({()[[][][]]}){}}))){}}]}{}{({((){{}[][]{}[][]{}}[{}])(())}[][])}",
68+
"()[[][()[]][]()](([[[(){()[[]](([]))}]]]))",
69+
"()[]({}{})(()){{{}}()()}({[]()}())[](){}(({()}[{}[{({{}}){({}){({})((({()})))}}}]]))",
70+
"}[{){({}({)})]([}{[}}{[(([])[(}){[]])([]]}(]]]]{][",
71+
"[{]{[{(){[}{}(([(]}])(){[[}(]){(})))}}{{)}}{}][({(}))]}({)",
72+
")})[(]{][[())]{[]{{}}[)[)}[]){}](}({](}}}[}{({()]]",
73+
"[[[({[]}({[][[[[][[{(()[][])}()[][]][]{}]]]]}))][(()){}]]]()[{}([]{}){}{{}}]",
74+
"({[]({[]})}())[][{}[{{(({{{([{}])}}}))}}]]",
75+
"([((()))()])[][][]{}()(([]))[]()[]((){}[]){}(){{}[]}[[{[]}]]",
76+
"[[(((({}{[]{}()}){}{{}}){({[]{[{}]{(){}(((){()}))}()}}[[]]()()[()])[[{}{}]()]}))]]{}[]{}({({{}})})",
77+
"(]{()}((", "[][(())[({{{()[]}}{[[][[][[[]{{{[()]{{{{}{[]}[][]}}}}}}]]]]}})]]", "}[})})}[)]{}{)",
78+
"({(}{})))}(}[)[}{)}}[)[{][{(}{{}]({}{[(})[{[({{[}{(]]})}", "]}})[]))]{][])[}(])]({[]}[]([)",
79+
"[{{}{[{{[}[[}([]",
80+
"[([]){}][({})({[(([])[][])][[{}{([{{}{(()){{{({}{{}}())}}[]}}()[()[{{{([](()){[[[]]]})}}}]]}])}]]})]",
81+
"]{}{(}))}](})[{]]()(]([}]([}][}{]{[])}{{{]([][()){{})[{({{{[}{}](]}}",
82+
"{[{}}){(}[][)(}[}][)({[[{]}[(()[}}){}{)([)]}(()))]{)(}}}][", "(]{}{(}}}[)[",
83+
"[]{}{[[]]}([{}]{}[]){{(())}}",
84+
"[)([{(][(){)[)}{)]]}}([((][[}}(]{}]]}]][(({{{))[[){}{]][))[]{]][)[{{}{()]){)])))){{{[(]}[}}{}]",
85+
"{({(){[[[][]{}[[([]{})]{}]][[]()()]]}})}[{}{{}}]", ")}][(})){))[{}[}",
86+
"{[]{({]}[}}[{([([)([){{}{(}}[]}}[[{[}[[()(])[}[]", "()()()[]", "((){}])][]][}{]{)]]}][{]}[)(])[}[({(",
87+
")[((])(]]]]((]){{{{())]}]}(}{([}(({}]])[[{){[}]{{}})[){(",
88+
"}][[{[((}{[]){}}[[[)({[)}]]}(]]{[)[]}{}(){}}][{()]))})]][(((}}",
89+
"([]){}{{}{}}()([([{}{[[]()([(([]()))()[[]]])]}])])",
90+
"[()[[]{{[]}()([])}[]][][]][]()[]{}{}[][]{}{}[()(){}]",
91+
"{[{){]({(((({](]{([])([{{([])[}(){(]](]{[{[]}}())[){})}))[{})))[",
92+
"{}[()[]][]{}{}[[{{[[({})]()[[()]]]}}]]", "{[{}[][]]}[((()))][]({})[]{}{()}", "(){[{({})}]}",
93+
"([]])][{)]({)[]))}]())[}]))][}{(}}})){]}]{[)}(][})[[", "((({{}(([{}(())]))[()]{[[[]()]]}})))",
94+
"}()))}(}]]{{})}][{](]][{]{[[]]]}]]}([)({([))[[(]}])}[}(([{)[)]]([[](]}]}{]{{})[]){]}{])(",
95+
"{}{}{}{[[()]][]}", ")]}]({{})[[[{]{{{}}][))]{{", "))){({}])}])}}]{)()(}(]}([",
96+
"([[]][])[[]()][]()(([[]]{[()[]{[][{}]}[()]]{}{[]}}{{}()}(()[([][]{})[[{}][]]{}[]])))",
97+
"(]{[({}[){)))}]{[{}][({[({[]))}[}]}{()(([]{]()}})}[]{[)](((]]])([]}}]){)(([]]}[[}[",
98+
"([[]])({}(([(){{}[{}]}]){[{}]}))[][{}{}](){}",
99+
"[][][][][][([])][]{({()}[[()()]{([(){[]{}}{(())}{[](){}()({}())}[({}[[]()])][]])}])}",
100+
"}[{{(}})}}(((())()({]{([]((][(({)[({[]]}[])}]{][{{}]{)][}(])}}}))}}}",
101+
"[]({})()[]{}{}[]({}{})[]{([])()[()][{()({})[{}{[[()]{}[]][]}(({{[]{()()()}{}[]()}[]}){{}{}})]}]}",
102+
"{{(([{)]{}({][{](){({([[[][)}[)})(", "[{}]{[()({[{}]})]}", "[[{}]]",
103+
"]{{({[{]}[[)]]}{}))}{){({]]}{]([)({{[]){)]{}){){}()})(]]{{])(])[]}][[()()}",
104+
"{[([}[[{{(]]][}()())]{){(){)]]){})}]{][][(}[]())[}[)})})[][{[)[})()][]))}[[}",
105+
"]()])}[}}}{]]{)[}(}]]])])}{(}{([{]({)]}])(})[{}[)]])]}[]{{)){}{()}]}((}}{({])[}])[]}",
106+
"(]}[{}{{][}))){{{([)([[])([]{[", "{(()[]){}}(){[]}({{}(()())})([]){}{}(())()[()]{}()",
107+
"{{}[{}[{}[]]]}{}({{[]}})[[(){}][]]{}(([]{[][]()()}{{{()()}{[]}({}[]{()})}{()}[[]][()]}))",
108+
"{[][]}[{}[](){}]{{}{[][{}]}}", "()(){}(){((){}[])([[]]())}", "{}[[{[((}[(}[[]{{]([(}]][[",
109+
"{}[([{[{{}()}]{}}([[{}[]]({}{{()}[][][]})])])]", "{[](}([)(])[]]})()]){[({]}{{{)({}(][{{[}}(]{",
110+
"[][]{{}[](())}{}({[()]}())[][[][({}([{}]))]]",
111+
"((()))[]{[(()({[()({[]}{})]}))]}{[]}{{({}{})[{}{}]{()([()])[{()}()[[]{}()]{}{}[]()]}[[]{[]}([])]}}" },
112+
113+
expectedList = new String[] { "YES", "YES", "NO", "NO", "NO", "NO", "YES", "YES", "YES", "NO", "YES",
114+
"YES", "YES", "YES", "NO", "NO", "YES", "YES", "YES", "NO", "NO", "NO", "YES", "YES", "YES",
115+
"YES", "NO", "YES", "NO", "NO", "NO", "NO", "YES", "NO", "NO", "NO", "YES", "NO", "YES", "NO",
116+
"NO", "YES", "NO", "NO", "NO", "YES", "YES", "NO", "YES", "YES", "YES", "NO", "YES", "NO",
117+
"YES", "NO", "NO", "YES", "NO", "YES", "YES", "NO", "YES", "NO", "YES", "YES", "NO", "NO", "NO",
118+
"NO", "YES", "YES", "YES", "YES", "NO", "YES", "NO", "YES", "YES" };
119+
120+
String actual = "", expected = "";
121+
122+
for (int i = 0; i < expression.length; i++) {
123+
124+
actual = alg.solve(expression[i]);
125+
expected = expectedList[i];
126+
127+
Assert.assertEquals(expected, actual);
128+
129+
System.out.println(actual);
130+
}
131+
132+
}
133+
}

0 commit comments

Comments
 (0)