Difference between substring and subsequence ?
substring is continuous, while subsequence is not continuous
1. Longest Palindrome Substring - example - ababd
Approach
1. Brute force - create all the substring, so create the substring of length 1 , length 2 , length 3, length 4 , and length 5. and then check for the string that has the maximum length .
Time complexity is O(N^3)
2. Decision Tree -
3. Dynamic programming using the TABULAR Approach
for d = 0; d < noOfRowSize; d++ // Row major
for i =0, j = d; i < noOfRowsSize, j < noOfColumnsSize ; i++ , j++
if (i == j || d i.e. Diagonal is Zero ) // that means they are equal
DP[i][j] = 1
MAX = 1
else if( j - 1 == 1) // length diff is ONE
if(ARR[i] == ARR[j])
DP[i][j] = 1
MAX = 2
else
if( ARR[i] == ARR[j] && DP[i+1][j-1] == 1 )
DP[i][j] = 1
MAX = j - 1 + 1
How to calculate the MAX length
4. Expand from middle
Row Major & Column Major iteration
Diagonal iteration -
for d = 0; d < noOfRowSize; d++
for i =0, j = d; j < noOfColumnsSize ; i++ , j++
2. Longest Palindrome Subsequence
Pattern Matching Algorithm
0. Brute force or Naïve Search , String matching algorithm.
1. Rabin Karp algorithm is an pattern matching or the search algorithm, using the hashing algorithm.
Algo -
a. We need to assign an numerical value to each character, in this case we can use the ACSCII value for each character.
b. Hash Function (why this hash function or the spurious value - The reason is collision , hence we are multiplying the value with 10, to avoid the collision)
hash value for pattern(p) = Σ(value * pow(10, position) )
example
pattern = abc
text = xyzabc
suppose the value for a = 1, b = 2, c = 3
then the pattern value would be
(a * 10 ^ 2) + (b * 10 ^ 1) + (c * 10 ^ 0)
which means
(1 * 10 ^ 2) + (2 * 10 ^ 1) + (3 * 10) = 100 + 20 + 3 = 123
c. Sliding window logic so that we don't recompute the value again
d. comparison
2. KMP Algorithm
3. Z - Algorithm
text = "batsdjfkljsljdfklsjkldjlfkjsldjbat" pattern = "bat"
Implement ATOI function
ATOI is an function in the C programming language that converts a string into an integer numerical representation.
1. check if the first character is minus, then toggle on the flag
2. Java trick to get the ASCII value (int)str.getCharAt(1) , will return the integer or the numerical value , if the int value is in between 0 and 9 then process else skip the loop
3. Now how to compute, the very old trick
ASCII Value (NUMBER) - ASCII Value(0) will return the integer
multiply each integer with the Power of 10 ^ n , incrementally in every loop and then SUM
Longest Common Prefix
1. The easiest solution is BRUTE Force algorithm
Complexity - O(No of Strings * length of shortest string)
2. TRIE implementation
Longest Common Subsequence
1. using recursion
2. DP
Comments
Post a Comment