Added 27 solutions

This commit is contained in:
rshaghoulian
2017-03-05 00:26:42 -06:00
parent af8c2969a4
commit 3a6797a94a
43 changed files with 2823 additions and 598 deletions
@@ -0,0 +1,38 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.Stack;
// Runtime: O(n)
// Space Complexity: O(n)
public class Solution {
public static void main(String[] args) {
/* Read and save input */
Scanner scan = new Scanner(System.in);
String str = scan.next();
scan.close();
/* Iterate through String, creating final result in a Stack */
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
Character ch = str.charAt(i);
if (!stack.isEmpty() && ch == stack.peek()) {
stack.pop(); // since String has 2 adjacent equal characters
} else {
stack.push(ch);
}
}
/* Print final result */
if (stack.isEmpty()) {
System.out.println("Empty String");
} else {
for (char ch : stack) {
System.out.print(ch);
}
}
}
}