Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 1fa06e3

Browse files
committed
Merge pull request #86 from andrewkshim/master
Spiral Java solution
2 parents a1babba + 84f00fb commit 1fa06e3

File tree

5 files changed

+257
-0
lines changed

5 files changed

+257
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
5+
public class AnagramDetection {
6+
7+
private Map<Character, Integer> charToPrimeMap;
8+
9+
public AnagramDetection() {
10+
charToPrimeMap = new HashMap<Character, Integer>();
11+
populateCharToPrimeMap();
12+
}
13+
14+
private boolean isPrime(int n) {
15+
for (int i = 2; i < Math.ceil(Math.sqrt(n)); ++i) {
16+
if (n % i == 0) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
23+
private int getNthPrime(int n) {
24+
int nthPrime = 2;
25+
while(n > 0) {
26+
if (isPrime(nthPrime)) {
27+
--n;
28+
}
29+
++nthPrime;
30+
}
31+
return nthPrime;
32+
}
33+
34+
private void populateCharToPrimeMap() {
35+
for (int i = 0; i < 52; i += 2) {
36+
charToPrimeMap.put((char)('a'+i/2), getNthPrime(i/2));
37+
charToPrimeMap.put(Character.toUpperCase((char)('a'+i/2)), getNthPrime(i/2+1));
38+
}
39+
}
40+
41+
private int hashString(String input) {
42+
int hash = 1;
43+
for (char c: input.toCharArray()) {
44+
hash *= charToPrimeMap.get(c);
45+
}
46+
return hash;
47+
}
48+
49+
public int detectAnagrams(String original, String anagram) {
50+
char[] originalChars = original.toCharArray();
51+
int anagramCount = 0;
52+
for (int i = 0; i < original.length() - anagram.length(); ++i) {
53+
String currentSubstring = String.copyValueOf(originalChars, i, anagram.length());
54+
if (hashString(currentSubstring) == hashString(anagram)) {
55+
++anagramCount;
56+
}
57+
}
58+
return anagramCount;
59+
}
60+
61+
public static void main(String[] args) {
62+
AnagramDetection a = new AnagramDetection();
63+
System.out.println(a.detectAnagrams("AdnBndAndBdaBn", "dAn"));
64+
System.out.println(a.detectAnagrams("AbrAcadAbRa", "cAda"));
65+
}
66+
67+
68+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashMap;
4+
5+
6+
public class ArrayPairSum {
7+
8+
public ArrayList<int[]> getAllSumPairs(int k, int[] input) {
9+
ArrayList<int[]> allSumPairs = new ArrayList<int[]>();
10+
HashMap<Integer, Boolean> usedNumbers = new HashMap<Integer, Boolean>();
11+
for (int i = 0; i < input.length; ++i) {
12+
int difference = k - input[i];
13+
if (usedNumbers.containsKey(difference) && !usedNumbers.get(difference)) {
14+
int[] sumPair = new int[2];
15+
sumPair[0] = input[i];
16+
sumPair[1] = difference;
17+
allSumPairs.add(sumPair);
18+
usedNumbers.put(input[i], true);
19+
} else {
20+
usedNumbers.put(input[i], false);
21+
}
22+
}
23+
return allSumPairs;
24+
}
25+
26+
public static void printAllPairSums(ArrayList<int[]> allPairSums) {
27+
String output = "[ ";
28+
for (int[] pairSum: allPairSums) {
29+
output += Arrays.toString(pairSum);
30+
output += " ";
31+
}
32+
output += " ]";
33+
System.out.println(output);
34+
}
35+
36+
public static void main(String[] arg) {
37+
ArrayPairSum a = new ArrayPairSum();
38+
int[] input1 = { 3, 4, 5, 6, 7 };
39+
printAllPairSums(a.getAllSumPairs(10, input1));
40+
int[] input2 = { 3, 4, 5, 4, 4 };
41+
printAllPairSums(a.getAllSumPairs(8, input2));
42+
}
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.Stack;
4+
5+
6+
public class BalancedBrackets {
7+
8+
public boolean isBalanced(String s) {
9+
HashMap<Character, Character> bracketMap = new HashMap<Character, Character>();
10+
bracketMap.put('{', '}');
11+
bracketMap.put('[', ']');
12+
bracketMap.put('(', ')');
13+
Stack<Character> bracketStack = new Stack<Character>();
14+
for (char c: s.toCharArray()) {
15+
if (!bracketStack.isEmpty() && bracketMap.get(bracketStack.peek()) == c) {
16+
bracketStack.pop();
17+
} else if (bracketMap.containsKey(c)) {
18+
bracketStack.push(c);
19+
} else {
20+
return false;
21+
}
22+
}
23+
return bracketStack.isEmpty();
24+
}
25+
26+
public static void main(String[] args) {
27+
BalancedBrackets b = new BalancedBrackets();
28+
System.out.println(b.isBalanced("()[]{}(([])){[()][]}"));
29+
System.out.println(b.isBalanced("())[]{}"));
30+
System.out.println(b.isBalanced("[(])"));
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
3+
4+
public class ConvertArray {
5+
6+
public int getOriginalIndex(int currentIndex, int length) {
7+
return (currentIndex % 3) * length + currentIndex/3;
8+
}
9+
10+
public int[] convert(int[] input) {
11+
for (int i = 0; i < input.length; ++i) {
12+
int originalIndex = getOriginalIndex(i, input.length/3);
13+
while (originalIndex < i) {
14+
originalIndex = getOriginalIndex(originalIndex, input.length/3);
15+
}
16+
int temp = input[i];
17+
input[i] = input[originalIndex];
18+
input[originalIndex] = temp;
19+
}
20+
return input;
21+
}
22+
23+
public static void main(String[] args) {
24+
ConvertArray c = new ConvertArray();
25+
int[] test1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
26+
System.out.println(Arrays.toString(c.convert(test1)));
27+
28+
}
29+
}

problems/spiral/Spiral.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.util.Arrays;
2+
3+
public class Spiral {
4+
5+
private enum Direction {
6+
UP, DOWN, LEFT, RIGHT
7+
}
8+
9+
private boolean isCoordinateValid(int height, int width, int row, int column) {
10+
return (row > 0 && row <= height) && (column > 0 && column <= width);
11+
}
12+
13+
private int getNumberAtCoordinate(int width, int row, int column) {
14+
return column + (row - 1) * width;
15+
}
16+
17+
private int[] getNextCoordinate(int row, int column, Direction currentDirection) {
18+
int[] nextCoordinate = new int[2];
19+
nextCoordinate[0] = row;
20+
nextCoordinate[1] = column;
21+
switch (currentDirection) {
22+
case UP:
23+
nextCoordinate[0] -= 1;
24+
break;
25+
case LEFT:
26+
nextCoordinate[1] -= 1;
27+
break;
28+
case DOWN:
29+
nextCoordinate[0] += 1;
30+
break;
31+
case RIGHT:
32+
nextCoordinate[1] += 1;
33+
break;
34+
}
35+
return nextCoordinate;
36+
}
37+
38+
private Direction getNextDirection(Direction currentDirection) {
39+
switch (currentDirection) {
40+
case UP:
41+
return Direction.LEFT;
42+
case LEFT:
43+
return Direction.DOWN;
44+
case DOWN:
45+
return Direction.RIGHT;
46+
case RIGHT:
47+
return Direction.UP;
48+
default:
49+
return currentDirection;
50+
}
51+
}
52+
53+
public int[] spiral(int height, int width, int row, int column) {
54+
int numberOfElements = height * width;
55+
int[] output = new int[numberOfElements];
56+
int outputIndex = 0;
57+
Direction currentDirection = Direction.UP;
58+
int stepsTaken = 0;
59+
int stepsNeeded = 1;
60+
while (outputIndex < numberOfElements) {
61+
if (isCoordinateValid(height, width, row, column)) {
62+
output[outputIndex++] = getNumberAtCoordinate(width, row, column);
63+
}
64+
int[] nextCoordinate = getNextCoordinate(row, column, currentDirection);
65+
row = nextCoordinate[0];
66+
column = nextCoordinate[1];
67+
++stepsTaken;
68+
if (stepsTaken == stepsNeeded) {
69+
if (currentDirection == Direction.LEFT || currentDirection == Direction.RIGHT) {
70+
++stepsNeeded;
71+
}
72+
stepsTaken = 0;
73+
currentDirection = getNextDirection(currentDirection);
74+
}
75+
}
76+
return output;
77+
}
78+
79+
public static void main(String[] args) {
80+
Spiral s = new Spiral();
81+
System.out.println(Arrays.toString(s.spiral(5, 5, 3, 3)));
82+
System.out.println(Arrays.toString(s.spiral(2, 4, 1, 2)));
83+
84+
}
85+
}

0 commit comments

Comments
 (0)