C Program to Check if a Given String is Palindrome
Recursive function to check if a string is palindrome
Longest Palindromic Substring
Given a string, find the longest substring which is palindrome.
For example,
Input: Given string :"forgeeksskeegfor", Output: "geeksskeeg" Input: Given string :"Geeks", Output: "ee"
Method 2: Dynamic Programming.
Approach: The time complexity can be reduced by storing results of sub-problems. The idea is similar to this post.
- Maintain a boolean table[n][n] that is filled in bottom up manner.
- The value of table[i][j] is true, if the substring is palindrome, otherwise false.
- To calculate table[i][j], check the value of table[i+1][j-1], if the value is true and str[i] is same as str[j], then we make table[i][j] true.
- Otherwise, the value of table[i][j] is made false.
- We have to fill table previously for substring of length = 1 and length =2 because
as we are finding , if table[i+1][j-1] is true or false , so in case of
(i) length == 1 , lets say i=2 , j=2 and i+1,j-1 doesn’t lies between [i , j]
(ii) length == 2 ,lets say i=2 , j=3 and i+1,j-1 again doesn’t lies between [i , j].
Below is the implementation of the above approach:
Output:
Longest palindrome substring is: geeksskeeg Length is: 10
Complexity Analysis:
- Time complexity: O(n^2).
Two nested traversals are needed. - Auxiliary Space: O(n^2).
Matrix of size n*n is needed to store the dp array.
An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory efficient version of Doubly Linked List can be created using only one space for address field with every node. This memory efficient Doubly Linked List is called XOR Linked List or Memory Efficient as the list uses bitwise XOR operation to save space for one address. In the XOR linked list, instead of storing actual memory addresses, every node stores the XOR of addresses of previous and next nodes.
Consider the above Doubly Linked List. Following are the Ordinary and XOR (or Memory Effiecient) representations of the Doubly Linked List.
Ordinary Representation:
Node A:
prev = NULL, next = add(B) // previous is NULL and next is address of B
Node B:
prev = add(A), next = add(C) // previous is address of A and next is address of C
Node C:
prev = add(B), next = add(D) // previous is address of B and next is address of D
Node D:
prev = add(C), next = NULL // previous is address of C and next is NULL
XOR List Representation:
Let us call the address variable in XOR representation npx (XOR of next and previous)
Node A:
npx = 0 XOR add(B) // bitwise XOR of zero and address of B
Node B:
npx = add(A) XOR add(C) // bitwise XOR of address of A and address of C
Node C:
npx = add(B) XOR add(D) // bitwise XOR of address of B and address of D
Node D:
npx = add(C) XOR 0 // bitwise XOR of address of C and 0
Traversal of XOR Linked List:
We can traverse the XOR list in both forward and reverse direction. While traversing the list we need to remember the address of the previously accessed node in order to calculate the next node’s address. For example when we are at node C, we must have address of B. XOR of add(B) and npx of C gives us the add(D). The reason is simple: npx(C) is “add(B) XOR add(D)”. If we do xor of npx(C) with add(B), we get the result as “add(B) XOR add(D) XOR add(B)” which is “add(D) XOR 0” which is “add(D)”. So we have the address of next node. Similarly we can traverse the list in backward direction.
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of memory-efficient doubly linked list. We will mainly discuss the following two simple functions.
- A function to insert a new node at the beginning.
- A function to traverse the list in forward direction.
In the following code, insert() function inserts a new node at the beginning. We need to change the head pointer of Linked List, that is why a double pointer is used (See this). Let us first discuss few things again that have been discussed in the previous post. We store XOR of next and previous nodes with every node and we call it npx, which is the only address member we have with every node. When we insert a new node at the beginning, npx of new node will always be XOR of NULL and current head. And npx of the current head must be changed to XOR of new node and node next to the current head.
printList() traverses the list in forward direction. It prints data values from every node. To traverse the list, we need to get pointer to the next node at every point. We can get the address of next node by keeping track of current node and previous node. If we do XOR of curr->npx and prev, we get the address of next node.
Following are the nodes of Linked List: 40 30 20 10
Given an array of size n, write a program to check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.
Examples:
Input : 20 21 45 89 89 90 Output : Yes Input : 20 20 45 89 89 90 Output : Yes Input : 20 20 78 98 99 97 Output : No
Recursive approach:
The basic idea for the recursive approach:
1: If size of array is zero or one, return true. 2: Check last two elements of array, if they are sorted, perform a recursive call with n-1 else, return false. If all the elements will be found sorted, n will eventually fall to one, satisfying Step 1.
Below is the implementation using recursion:
Output:
Yes
Time Complexity: O(n)
Auxiliary Space: O(n) for Recursion Call Stack.
Another Recursive approach:
Yes
Time Complexity: O(n)
Auxiliary Space: O(n) for Recursion Call Stack.
Iterative approach: The idea is pretty much the same. The benefit of the iterative approach is it avoids the usage of recursion stack space and recursion overhead.
Below is the implementation using iteration:
Output:
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)
Given a tree, our task is to check whether its left view is sorted or not. If it is then return true else false.
Examples:
Input:
Output: true
Explanation:
The left view for the tree would be 10, 20, 50 which is in sorted order.
Approach:
To solve the problem mentioned above we have to perform level order traversal on the tree and look for the very first node of each level. Then initialize a variable to check whether its left view is sorted or not. If it is not sorted, then we can break the loop and print false else loop goes on and at last print true.
true
Time complexity: O(N)
Comments
Post a Comment