Kamy
BAN USERThe question is : are we looking for unique words of 1 length? or not?
Assume we have the answer and we are taking care of this in every approach.
First Approach:
- Split by " " String[] token = input.split(" ") , go word by word and if it is of length 1 count++;
// it uses O(nr of words) additional space
SecondApproach:
- go element by element and if it is surround it by " " or if it is at the beginning of the string and followed by " " or it at the end of a string and preceded by " "
- if so count++;
// this one uses O(1) space
Third approach:
-use pattern matching:
Pattern p = Pattern.compile("\\w+");
Matcher m = p.matcher(input);
while(m.find()){
if(m.group().length()==1) count++;
}
I think that you have created in class X:
--1-- a new class Y
AND
--2-- an object named Y of the class W
X.Y.Z calls (2) , which is a variable = an object named Y of class W
That's why it prints "Orange"
If you wanted to print "Apple" you should use :
X.Y obj = new X.Y();
System.out.println(obj.Z);
This calls on object of the class Y .
yes I am taking care of operator precedency :
I have 2 functions :
1) int computeWholeExp(String input)
- gets the input , puts the numbers in Stack N and operators and parentheses in Stack O.
- every time we find ')' we extract in two temporary stacks num and op all the numbers and operators until I pop I '(' , which I will not put in op.
- will call evaluateExp(num, op) and put the result into N.
- go forward in the input and repeat the previous steps if needed
- when we finished the input we will call evaluateExp(N, O)
and return the result.
2) evaluateExp(Stack n, Stack op)
- also takes care of operator precedence and negative numbers
You can have something like this:
O
/ \
O O
/ \ / \
X P1 P2 P3
X is the current node. We are interested to find the right node for X.
P1 , P2 , P3 and null are the possibilities.
so I think you should keep in mind the level on which you are interested to find the "right" node. Use another variable to keep track of the current level. Go test each of the three possibilities and if non of them is !null then return null which means that you have no "right" node.
I don't think you can do that, because a phone number is assigned to one person , but one person can have more than one phone number. What I am trying to say here is that a map has unique keys.
You can do that only if number to name is HashMap<String, String> and name to number is HashMap<String, ArrayList<String>>. This will work.
I would construct the triangle bottom - up. Memorize it into a ArrayList and when you want to print it go from the end of the ArrayList to the beginning.
You can see that each row starts with k stars ( the last row has 0, the n-1 has 1, the n-2 has 2 and so on).
You also can see that inside a row , between 2 numbers there is only one star.
Use this and you have your solution.
Hi!
I would use two stacks :
- numbers (will contain only numbers)
- operators (will contain : -, +, *, /, % ,(, ) )
I will go char by char on the input string. Every time I find a number I will push it into Numbers, every time I find an operator or '(' I will push it into the Operators Stack. Every time I find a ')' I will extract from the Numbers and from the Operators Stack until I meet '('. I will evaluate the expression got , and insert the result into the Numbers Stack.
After I am done evaluating the input, I will go through my stack and compute the expression that now has no parentheses.
Q: How will I evaluate an expression that doesn't contain parentheses?
A: I will do it in a function like : public int evaluateExp(String exp){...}
- I will use 2 stacks: num and op (^_^)
- in num I will insert all the numbers I meet in the exp
- in op I will insert all the operators I meet in the exp, but every time I meet * or / or % I will extract the last number pushed in the num , I will get the next number that appears in the exp and compute the operation with the current operator I have. Next, insert the result into numbers and continue evaluating the exp.
- when I have just one number into the numberStack and I am done evaluating the exp , I will return the head of the numberStack.
And Done!
Hi!
I would use a HashMap , where the key is the word sorted and as value I would have a ArrayList of words for which if we sort the letters we would obtain the key of their entry.
After going over each word of the list and putting it into a category , I would create an ArrayList<ArrayList<String>> . Going just once over the HashMap I will put into the ArrayList all the values from the HashMap, and return it.
Example :
Input : cat, bat, act, tab
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
--- after going once over each word of the input, map will contain:
{ act = {cat, act}; abt = {bat, tab} }
ArrayList<ArrayList<String>> out = new ArrayList<ArrayList<String>>();
foreach key in map {
out.add(map.get(key));
}
return out;
Complexity time: O(n* the max length of a word from the input list)
The idea is:
- I will keep in nextZero the position of the next Zero I will have to swap
- the number of swaps for a zero is the distance between the current position of a zero and the position this zero should be.
Complexity : time - O(n) space- O(1)
int nextZero = 0;
int count = 0;
while(bits[k] == 0) k++;
nextZero = k;
for(int i = k; i < bits.length; i++){
if(bits[i]==0){
count += i - nextZero;
nextZero++;
}
}
return count;
So this is O(n* w log w) time complexity? (w is the size of the max word)
- Kamy December 16, 2012