Is combined Strings of LinkedList of strings palindrom? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Question is: You have Singly linkedlist which contains String[any no of chars] in its node. Now How to find combined String[no need of building string from linkedlist] is palindrome or not ?
Example: ["abc"]-->["d"]-->["ed"]-->["c"]-->["ba"] --> is valid input linkedList
combined string would be- abcdedcba
This is what I have thought of:
1.Traverse all list by node and compute combined string length-N.
2.take two pointer point to First Node, P1, P2.
3.advance P1 till reach to string length >=N/2.[we have to remember previous total length] now advance char pointer within node string to point to N/2+2th char[if N is odd] or N/2+1th[if N is even].
4.Start from P2 with char pointer to first char of node string. and perform equal on chars.
5.If P1 reach to end with all char exhausted. then string is palindrome.
but in this approach i'm traversing linkedlist 2.5 times. which will be not good if LinkedList will be huge.
Can anyone suggest better approach.

Your solution is optimal, you cannot do anything with the list faster than O(n) (because the problem requires reading all its elements), and your solution is O(n). Traversing 1.5 times (not 2.5 times - one iteration to the half, and one from the half to begining and from the halt to the end) is also very small constant. If you do not want to rebuild the string I am pretty sure that there is no faster solution (Although your solution consumes N/2 additonal space, but it is unavoidable if you have just one-directional list).

Related

Implementatin of queue in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it better to use linked list over arraylist for implementaion of Queue?
If I dequeue , the over load would be less in the case of liked list implementation?
The time it takes to dequeue an element from the beginning of a LinkedList is less than from ArrayList.
This is because ArrayList is based on an array, and when a first element is removed all elements except the dequeued need to be shifted one position left. The greater the number of elements there is in ArrayList, the longer it will take.
In case of LinkedList, no matter how big the list is, there just constant number of references that should be updated in order to dequeue a first element.
Of course you can dequeue elements from the end of an ArrayList and it will take constant time (most of the time), but than addition of a new element (to the beginning) will require to shift all elements one position to the right.
There is already a LikedList queue implementation in Java. I did not analyse the code but I would guess that a LinkedList if faster than an ArrayList in dequeuing an entry.

Circular Queue using arrays with no count approach. (java) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've implemented it successfully with count, the book I'm using to learn just shows the code but without any proper explanation.
I've tried doing some search on google but can't find a material explaining it.
All what I've understood that Front and Rear points will assume the same location if the array is empty or full, so we are using an extra slot in the array "to do some sort of checking I don't understand to get it done".
Does anybody have a tutorial on this? I'm sorry if this comes as waiting for someone to feed me information, but I truly cannot find anything.
The math is pretty simple: if you have the beginning and the ending indexes, you can have three situations there:
The ending index is lower,
The beginning index is lower, and
The indexes are the same.
When the ending index is lower, say, b=10 and e=3, all the data is located between the two indexes. You can compute the count by subtracting the ending index from the beginning index, b-e: in my example, you've got seven items in the queue.
When the beginning index is lower, say, b=3 and e=10, then the data goes around the end of the queue. Say, the total size of your queue is N=100. Then the number of data items is b+N-e, or 3+100-10=93 elements.
The fact that you would be able to use at most N-1 elements follows from these two formulas: the highest number of entries that you can have is N-1 for both formulas, because the situation when all N entries are filled up is indistinguishable from the situation when the queue is empty.
You can check if your queue is full by comparing (b+1)%N to e. If they are the same, you cannot put any more data into your queue.

Best algorithm to find an element in an ordered list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
There is list A which contains numbers in ascending order.
Similarly a list B which also contains numbers in ascending order.
The result should be list C which contains numbers from A which are not in B.
My Solution:
I iterated through A and checked for the number in B using .contains() and added the required elements in C.
I was told using .contains() is higher order of complexity O(n).
Does anyone have a better solution?
Yes, your method will get O(n^2). The idea to get O(n) is just like merging two sorted lists into one. Merging two sorted lists into one is a Union. Finding common elements in two sorted lists is an Intersection. Your problem is to find the set difference of two sorted lists. Drawing a simple example on paper, you will find you can use two iterators to do it in O(n) time.
Use Merge Sort like merge routine to add element from A lists into C using following conditions:-
if (B->data<A->data) just iterate B
if(B->data==A->data) iterate A & B
if(A->data<B->data) Add A->data to C and iterate A
Time complexity O(N)

How to sort a queue in place in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
You are just not allowed to create new data structures.
Note: This is not a homework question, i am preparing for interviews and I came across this question
Pop two elements from the queue.
Compare them.
Push the lesser one in the queue.
Pop another element.
Keep repeating from step no. 2.
After n-1 comparisons, you will get the largest element in the queue.
Push it and repeat the above n-1 times. At each iteration, you need to make one less comparison as the last element is already the maximum.
This will sort the queue in descending order. For ascending order, simply reverse the comparison results.
Note: The above algorithm is incomplete and a small step is missing. I leave that to you to identify and fix.
Assuming taketakes an element from front of queue, offer inserts an element to fron of queue.
sort q :
return if q is empty
a = take q
sort q
insert a q
insert a q:
if q is empty
offer q a
return
b = take q
if a < b
offer q b
offer q a
return
insert a q
offer q b
return
poll() and offer() one by one every object (.size() times) and save the minimum (current minimum)
poll() and offer() one by one every object and scan for next bigger after current minimum. If you can't find next - stop.
poll() and offer() one by one untill you offer your next after current minimum, don't poll it
poll() and offer() one by one untill you poll you current minimum
poll() you next after current minimum. It's your current minimum now. Go to step 2

Return True if Arrays have same elements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I could use some help comparing two arrays, previously created in another method. They are called enterNumbers, user input array and drawNumbers, a randomly generated array.
My method header should look like this:
public static boolean containSameElements(int[] enterNumbers, int[] drawNumbers)
{
}
The method should compare the two arrays and return true if the numbers are the same, regardless of the order.
Not looking for the answer, just maybe a place to start.
Just sort them before
Arrays.sort(enterNumbers);
Arrays.sort(drawNumbers);
if(Arrays.equals(enterNumbers, drawNumbers)){
System.out.println("both are same");
}
Well, you can either
Create two histograms (using a hash based map/set) to count elements
in each array, and then compare the sets/maps. This solution is O(n)
space and O(n) time on average. Have a look at Map or Set for this. (Either you want Map or Set depends if existence of duplicates is important or not)
Another solution is sort and iterate. This is O(nlogn) time worst
case. Have a look on Arrays.sort() for this solution.
if (drawNumbers.length != enterNumbers.length)
return false;
List<Integer> base = new ArrayList<Integer>();
for (Integer i : drawNumbers)
base.add(i);
for (Integer i : enterNumbers)
base.remove(i);
return base.isEmpty();
This is a very common "problem" which can be solved using different methods. If I understand you correctly, all of the numbers have be inside the both arrays, but they don't have to be at the same indexes?
Then you can just make a while/for loop with (with two counters; one for each array) and check if the number on index 0 in the first array equals any of the numbers in the second array. If it doesn't the while/for-loop is done and the test failed. If it does go on to the next index in the first array. Continue until everything is tested(all numbers in first array versus the second array) or until a number doesn't exist in both arrays. Good luck

Categories