Strings Flashcards

1
Q

Longest Common Prefix

A

easy method is to use associative property and calculate common prefix one by one for each common string. O(mn)

find the string with least length
check for each character it is present in each string and append return the result when mistmatched o (MN)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Implement strtsrt

A

Standard O(mn) solution where we check the inner string in from all points when return i using j as moving pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Implement atoi (string to int) “123” to 123

A
  1. remove first whitespaces
  2. check for the sign
  3. add the number = number * 10 + (int)string[i] ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Longest Distinct Characters in the string

A

We go from left to right maintaining max_len.

foreach char we check if it was found previously.
if not found we maintain the index of this char in an array or map and increase cur_len
else
we calculate the leng with i - prev_index
and update if greater than max_len

lastly we check again if the cur_len is greater thjan max_len

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

two strings are anagrams of each other or not

A
  1. use sorting and check if each character is same or not
  2. Count the characters using char arrays
  3. Use xoring
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Form a palindrome

A

we use dp here to because the recursive method has overlapping subprblems,

we fill the table in a gap structure
this is the dp condtion
for(gap = 1; gap < n; gap++){
for(l - 0; h = gap; h < n; h++){
table[l,h] = str[l] == str[h]? table[l + 1, h -1] : Math.Min(table[l, h-1],table[l +1,h]) + 1;
}
}

check once after filling table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Remove Duplicates

A

use hash set and convert it into a strnig. o(N)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly