Can anyone tell me what I'm doing wrong? - Stacks - java

I am required to write a method, compress to do the following;
The objective of the
method compress is to remove all null elements from the stack s1. The remaining (non-null)
elements should be kept on s1 in their initial order. The auxiliary stack s2 should be used as
a temporary storage for the elements from s1. At the end of the computation of this method,
stack s2 should have the same content as at the beginning of the computation. See the method
main for an example of the expected behaviour of the method compress.
So far I have;
import net.datastructures.ArrayStack;
import net.datastructures.Stack;
public class Stacks {
public static <E> void compress(Stack<E> S1, Stack<E> S2) {
int counter = 0;
while (!S1.isEmpty()) {
}
if (S1.top() == null) {
S1.pop();
} else if (S1.top() != null) {
S2.push(S1.pop());
counter++;
}
for (int i = counter; i < counter; i++) {
S2.push(S1.pop());
}
}
public static void main(String[] args) {
// test method compress
Stack<Integer> S1 = new ArrayStack<Integer>(10);
S1.push(2);
S1.push(null);
S1.push(null);
S1.push(4);
S1.push(6);
S1.push(null);
Stack<Integer> S2 = new ArrayStack<Integer>(10);
S2.push(7);
S2.push(9);
System.out.println("stack S1: " + S1);
// prints: "stack S1: [2, null, null, 4, 6, null]"
System.out.println("stack S2: " + S2);
// prints: "stack s2: [7, 9]"
compress(S1, S2);
System.out.println("stack S1: " + S1);
// should print: "stack S1: [2, 4, 6]"
System.out.println("stack S2: " + S2);
// should print: "stack S2: [7, 9]"
}
}
I can't figure out where I'm going wrong, the code prints the two lines before the compress method and then prints nothing.

while (!S1.isEmpty()) {
}
Right there, you have an infinite loop.

i guess writing the if...else inside your while() as below
while (!S1.isEmpty()) {
if (S1.top() == null) {
S1.pop();
} else if (S1.top() != null) {
S2.push(S1.pop());
counter++;
}
}
and inside your for it must be something like
for (int i = counter; i < counter; i++) {
S1.push(S2.pop());
}
guess it should work

I spotted two mistakes in your code. One on each of your cycles.
Your while loop should be wrapping the conditionals right after
it.
Your for loop has its increment variable badly assigned.
Correct version should be:
public static <E> void compress(Stack<E> S1, Stack<E> S2) {
int counter = 0;
while (!S1.isEmpty()) {
if (S1.top() == null) {
S1.pop();
} else {
S2.push(S1.pop());
counter++;
}
}
for (int i = 0; i < counter; i++) {
S1.push(S2.pop());
}
}
Edit: An equivalent for loop (maybe you were trying to write this one) may be as follows.
for (int i = counter; i > 0; i--) {
S1.push(S2.pop());
}
2nd edit: The variables in the for loops were switched (S2 was in S1's place and vice versa).

Related

Decode String in Java

I am trying to convert this Python Solution in Java. For some reason, my Java Solution is not working. How can this be done correctly?
https://leetcode.com/problems/decode-string/description/
Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4].
The test cases are generated so that the length of the output will never exceed 105.
Example 1:
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Example 2:
Input: s = "3[a2[c]]"
Output: "accaccacc"
Python Solution:
class Solution:
def decodeString(self, s: str) -> str:
stack = []
for char in s:
if char is not "]":
stack.append(char)
else:
sub_str = ""
while stack[-1] is not "[":
sub_str = stack.pop() + sub_str
stack.pop()
multiplier = ""
while stack and stack[-1].isdigit():
multiplier = stack.pop() + multiplier
stack.append(int(multiplier) * sub_str)
return "".join(stack)
Java Attempt:
class Solution {
public String decodeString(String s) {
Deque<String> list = new ArrayDeque<String>();
String subword = "";
String number = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ']' ) {
list.add(String.valueOf(s.charAt(i)));
}
else {
subword = "";
while (list.size() > 0 && !list.getLast().equals("[") ) {
subword = list.pop() + subword;
}
if (list.size() > 0) list.pop();
number = "";
while (list.size() > 0 && isNumeric(list.getLast())){
number = list.pop() + number;
}
for (int j = 1; (isNumeric(number) && j <= Integer.parseInt(number)); j++) list.add(subword);
}
}
return String.join("", list);
}
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch(NumberFormatException e){
return false;
}
}
}
The reason why your posted code is not working is because the pop() method in python removes the last element by default.
But in Java, the ArrayDeque class's pop() method removes the first element.
In order to emulate the python code with the ArrayDeque, you'll need to use the removeLast() method of the ArrayDeque instance instead.
public class Solution{
public static String decodeString(String s) {
StringBuilder stack = new StringBuilder();
for(char c : s.toCharArray()) {
if(c != ']') {
stack.append(c);
} else {
StringBuilder sub_str = new StringBuilder();
while(stack.charAt(stack.length() - 1) != '[') {
sub_str.insert(0, stack.charAt(stack.length() - 1));
stack.deleteCharAt(stack.length() - 1);
}
stack.deleteCharAt(stack.length() - 1);
StringBuilder multiplier = new StringBuilder();
while(stack.length() > 0 && Character.isDigit(stack.charAt(stack.length() - 1))) {
multiplier.insert(0, stack.charAt(stack.length() - 1));
stack.deleteCharAt(stack.length() - 1);
}
for(int i = 0; i < Integer.parseInt(multiplier.toString()); i++) {
stack.append(sub_str);
}
}
}
return stack.toString();
}
public static void main(String[] args) {
System.out.println( decodeString("3[a2[c]]"));
//Output: "accaccacc"
System.out.println( decodeString("3[a]2[bc]"));
//Output: "aaabcbc"
}
}

Reverse a sentence in Java [duplicate]

This question already has answers here:
Reverse a given sentence in Java
(14 answers)
Closed 2 years ago.
The question for homework is the program should print the String in reverse word by word.
Your `String' should be assigned the value “pay no attention to that man behind the curtain” and should be printed as the sample output.
Getting errors compiling and been at this for 3 hours - lost!!
I must use the charAt method, the substring method and an if statement:
curtain
the
behind
man
that
to
attention
no
pay
public class backwards
{
public static void main(String args[])
{
String s1 = new String("pay no attention to that man behind the curtain");
/*int pos = s1.indexOf(' ');
while(s1.length() > 0)
{
if(pos == -1)
{
System.out.println(s1);
s1 = "";
}
else
{
System.out.println(s1.substring(0,pos));
s1 = s1.substring(pos+1);
pos = s1.indexOf(' ');
}
}*/
int pos = 0;
for(int i = s1.length()-1 ; i >= 0; i--)
{
// System.out.println("Pos: " + pos);
if(s1.charAt(i) == ' ')
{
System.out.println(s1.substring(i+1));
s1 = s1.substring(0,i);
}
else if(i == 0)
{
System.out.println(s1);
s1 = "";
}
}
}
}
You can do it simply as
public class Main {
public static void main(String[] args) {
// Split on whitespace
String[] arr = "pay no attention to that man behind the curtain".split("\\s+");
// Print the array in reverse order
for (int i = arr.length - 1; i >= 0; i--) {
System.out.println(arr[i]);
}
}
}
Output:
curtain
the
behind
man
that
to
attention
no
pay
Alternatively,
public class Main {
public static void main(String[] args) {
String s1 = "pay no attention to that man behind the curtain";
for (int i = s1.length() - 1; i >= 0; i--) {
if (s1.charAt(i) == ' ') {
// Print the last word of `s1`
System.out.println(s1.substring(i + 1));
// Drop off the last word and assign the remaining string to `s1`
s1 = s1.substring(0, i);
} else if (i == 0) {
// If `s1` has just one word remaining
System.out.println(s1);
}
}
}
}
Output:
curtain
the
behind
man
that
to
attention
no
pay

Boundary Case conditions for String Anagrams

I trying to write one string Anagram program but stuck while checking the boundary conditions.
I know there are lots of ways and programs available on internet related to String Anagrams using single loops or using collections framework, but I need the solution for my code that how can I involve boundary cases for the code.
public class StringAnagram {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abc";
String strAnagram = "cba";
boolean areAnagrams = ifAnagrams(str, strAnagram);
System.out.println(areAnagrams);
}
private static boolean ifAnagrams(String str, String strAnagram) {
// TODO Auto-generated method stub
int count = 0;
char[] a = strAnagram.toCharArray();
if (str.length() != strAnagram.length()) {
return false;
}
for (int i = 0; i < str.length(); i++) {
{
System.out.println("str.charAt(i) in outer loop :" + str.charAt(i));
for (int j = 0; j < strAnagram.length(); j++) {
if (str.charAt(i) == strAnagram.charAt(j)) {
System.out.println("str.charAt(i) : " + str.charAt(i));
System.out.println("strAnagram.charAt(j) : " + strAnagram.charAt(j));
count++;
}
}
}
System.out.println(count);
if (count == str.length()) {
return true;
}
}
return false;
}
}
Code is working fine if I am inputting the input likes -
"abc" or "abcd" where each char in string is occuring only one time, but it fails when input is like "aab" can be compared to "abc" and it will show strings are anagrams.
So, how this condition I can handle in my code. Please advice.
The problem with your solution is that it only checks if each character in the first string is present in the second string. There are 2 more conditions you need to consider:
If each character in the second string is also present in the first string
If character count for each character in the first and the second string matches
Your current solution will return True for input of ("aaa", "abc") while it should return False. Implementing the first condition I mentioned above will fix this problem.
After you implement the first condition, your solution will return True for input of ("abb", "aab") while it should return False. Implementing the second condition I mentioned above will fix this problem.
Here is a simple way to make this work:
Map<Character, Integer> charCount = new HashMap<Character, Integer>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (charCount.containsKey(c)) {
charCount.put(c, charCount.get(c)+1);
} else {
charCount.put(c, 1);
}
}
for (int i = 0; i < strAnagram.length(); i++) {
char c = strAnagram.charAt(i);
if (!charCount.containsKey(c)) return false;
if (charCount.get(c) == 0) return false;
charCount.put(c, charCount.get(c)-1);
}
for (char k : charCount.keySet()) {
if (charCount.get(k) != 0) return false;
}
return true;
Since there are no nested loops, the time complexity is O(n). Even though a Map is used, the space complexity is O(1), since it is guaranteed that the total number of keys will not exceed the number of all possible characters.
This solution is even better than sorting in terms of time and space complexity.
This may still be wildly inefficient. Again I apologize for initially overlooking your requirement that no collection frameworks could be included.
public class StringAnagram {
public static void main(String[] args) {
// TODO Auto-generated method stub
// String str = "abc";
// String strAnagram = "cba";
String str = "abcdd";
String strAnagram = "dccba";
boolean areAnagrams = ifAnagrams(str, strAnagram);
System.out.println(areAnagrams);
}
private static boolean ifAnagrams(String str, String strAnagram) {
int count = 0;
char[] a = strAnagram.toCharArray();
char[] b = str.toCharArray();
String alphaString = "abcdefghijklmnopqrstuvwxyz";
char[] alpha = alphaString.toCharArray();
System.out.println(a);
System.out.println(b);
System.out.println("");
if (str.length() != strAnagram.length()) {
return false;
}
for (int i=0; i < alpha.length; i++) {
int countA = 0;
int countB = 0;
for(int j = 0; j < a.length; j++){
if (a[j] == alpha[i]) {
countA++;
}
if (b[j] == alpha[i]) {
countB++;
}
}
if (countA != countB) {
return false;
}
}
return true;
}
}
This alternate solution makes use of a string that contains all the letters in the alphabet, and iterates through them to check if both strings have the same count of each letter. No frameworks this time :)

String permutation with recursion

I am a java beginner and trying to do a string permutation practice from java programming book. I am defining two method:
public static void displayPermutation(String s)
public static void displayPermutation(String s1, String s2)
The first method simply invokes displayPermutation(" ", s). The second method uses a loop to move a character from s2 to s1 and recursively invokes it with a new s1 and s2. The base case is that s2 is empty and prints s1 to the console.
Can anyone help me to find what is the problem of the following code?
Her's example:
public static void displayPermutation(String s) {
displayPermuatation("", s);
}
private static void displayPermuatation(String s1, String s2) {
//base case: when s2 is empty, print s1
if (s2.isEmpty()) {
System.out.println(s1);
}
else {
for (int i = 0; i < s2.length(); i++) {
//move a char from s1 to s2, and recursively invokes it with
//new s1 and s2
s1 = s1 + s2.charAt(i);
s2 = s2.substring(0, i) + s2.substring(i+1);
displayPermuatation(s1, s2);
}
}
}
if s = "abc",
it prints only:
abc
acb
it seems that in the first call of displayPermuatation("", "abc"), it does not finish the for loop....
any comments?
Thanks for all the comments below. I think the mistakes I made is because that passing object as argument to a method is actually passing the reference. it is not like primitive data (passing by value). When changing the object, it will affect following method call using that object.
Do not alter s1 and s2 in the loop, that causes the error. Simply pass those definitions as arguments to recursive function. Like this:
.
.
for (int i = 0; i < s2.length(); i++) {
displayPermuatation(s1 + s2.charAt(i), s2.substring(0, i) + s2.substring(i+1));
}
.
.
Problem with your code is that you are changing value of s1 and s2 in the loop which affects the following iterations in the loop, see the following code where I have fixed this issue.
public static void displayPermutation(String s) {
displayPermuatation("", s);
}
private static void displayPermuatation(String s1, String s2) {
// base case: when s2 is empty, print s1
if (s2.isEmpty()) {
System.out.println(s1);
} else {
for (int i = 0; i < s2.length(); i++) {
// move a char from s1 to s2, and recursively invokes it with
// new s1 and s2
displayPermuatation(s1 + s2.charAt(i), s2.substring(0, i) + s2.substring(i + 1));
}
}
}
Don't change the original values for s1, s2 in the loop:
private static void displayPermuatation(String s1, String s2) {
//base case: when s2 is empty, print s1
if (s2.isEmpty()) {
System.out.println(s1);
}
else {
for (int i = 0; i < s2.length(); i++) {
//move a char from s1 to s2, and recursively invokes it with
//new s1 and s2
string new_s1 = s1 + s2.charAt(i);
string new_s2 = s2.substring(0, i) + s2.substring(i+1);
displayPermuatation(new_s1 , new_s2 );
}
}

Permutations with duplicates

Before I start, I have to apologize for bringing up another case of permutations with duplicates. I have gone through most of the search results and can't really find what I am looking for. I have read about the Lexicographical order and have implemented it. For this question, I am suppose to implement a recursion method that prints out the all the strings of length n consisting of just the characters a and b that have an equal number of a and b’s. The strings must be printed out one line at a time in lexical order. So, for example, a call:
printBalanced(4);
will print the strings:
aabb
abab
abba
baab
baba
bbaa
here is the code
public static void main(String[] args){
printBalanced(4);
}
public static void printBalanced(int n){
String letters = "";
//string to be duplicates of "ab" depending the number of times the user wants it
for(int i =0; i<n/2;i++){
letters += "ab";
}
balanced("",letters);
}
private static void balanced(String prefix, String s){
int len = s.length();
//base case
if (len ==0){
System.out.println(prefix);
}
else{
for(int i = 0; i<len; i++){
balanced(prefix + s.charAt(i),s.substring(0,i)+s.substring(i+1,len));
}
}
}
My print results:
abab
abba
aabb
aabb
abba
abab
baab
baba
baab
baba
bbaa
bbaa
aabb
aabb
abab
abba
abab
abba
baba
baab
bbaa
bbaa
baab
baba
As you can see, I get a lot of duplicates. This is partly due to the requirement to use only characters 'a' and 'b'. The duplicates will not happen if it was "abcd" or "0123". I have read about using an arraylist and store all the results and then loop through N elements to check for duplicates and then removing it. This does not seem to be the best way to do it. Can someone share about other better solutions for this problem? =)
My Solution using SortedSet:
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class BalancedStrings {
public static void main(String[] args){
printBalanced(4);
}
public static void printBalanced(int n){
String letters = "";
for(int i =0; i<n/2;i++){
letters += "ab";
}
SortedSet<String> results = balanced("",letters);
Iterator<String> it = results.iterator();
while (it.hasNext()) {
// Get element and print
Object element = it.next();
System.out.println(element);
}
}
//This method returns a SortedSet with permutation results. SortedSet was chosen for its sorting and not allowing
//duplicates properties.
private static SortedSet<String> balanced(String prefix, String s){
SortedSet<String> set = new TreeSet<String>();
int len = s.length();
//base case
if (len == 0){
//return the new SortedSet with just the prefix
set.add(prefix);
return set;
}
else{
SortedSet<String> rest = new TreeSet<String>();
for(int i = 0; i<len; i++){
//get all permutations and store in a SortedSet, rest
rest = balanced(prefix + s.charAt(i),s.substring(0,i)+s.substring(i+1,len));
//put each permutation into the new SortedSet
set.addAll(rest);
}
return set;
}
}
}
This Solution does not require the extra space for sorting
credits: http://k2code.blogspot.in/2011/09/permutation-of-string-in-java-efficient.html
public class Permutation {
public static void printDuplicates(String str, String prefix) {
if (str.length() == 0) {
System.out.println(prefix);
}
else {
for (int i = 0; i < str.length(); i++) {
if (i > 0) {
if (str.charAt(i) == str.charAt(i - 1)) {
continue;
}
}
printDuplicates(
str.substring(0, i) + str.substring(i + 1, str.length()),
prefix + str.charAt(i)
);
}
}
}
public String sort(string str) {
// Please Implement the sorting function, I was lazy enough to do so
}
public static void main(String[] args) {
String test = "asdadsa";
test = sort(test);
printDuplicates(test, "");
}
}
You can use a Set and store the results in it (preferably SortedSet) this will eliminate duplicates and maintain a sorted order as well while traversal.
You can use the most common implementation of permutations (swap an element with the first and permute the rest). First build the string, sort it, then generate all possible permutations. Don't allow duplicates.
An implementation could be:
static String swap(String s, int i, int j) {
char [] c = s.toCharArray();
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
return String.copyValueOf(c);
}
static void permute(String s, int start) {
int end = s.length();
if(start == end) {
System.out.println(s);
return;
}
permute(s, start + 1);
for(int i = start + 1; i < end; i++) {
if(s.charAt(start) == s.charAt(i)) continue;
s = swap(s, start, i);
permute(s, start + 1);
}
}
public static void main(String [] args) {
String s = "aabb";
permute(s, 0);
}
Produces output:
aabb
abab
abba
baab
baba
bbaa
Based on the code of #0605002, I modified a little bit. In the for loop of permute, we need to swap again after the permute method call. It is like backtracking. We need to swap it back for next iteration.
static void permute(String s, int start) {
int end = s.length();
if(start == end) {
System.out.println(s);
return;
}
permute(s, start + 1);
for(int i = start + 1; i < end; i++) {
if(s.charAt(start) == s.charAt(i)) continue;
s = swap(s, start, i);
permute(s, start + 1);
s = swap(s, start, i);
}
}

Categories