Skip to content

Commit ff1a142

Browse files
committed
🚀 02-Sep-2020
1 parent 063d91c commit ff1a142

File tree

4 files changed

+282
-1
lines changed

4 files changed

+282
-1
lines changed

competitive programming/leetcode/14. Longest Common Prefix.cpp

+83-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ All given inputs are in lowercase letters a-z.
2424

2525

2626

27-
27+
// word by word matching O(n2)
2828

2929
class Solution {
3030
public:
@@ -44,3 +44,85 @@ class Solution {
4444
return res;
4545
}
4646
};
47+
48+
49+
50+
51+
// character by character by matching O(n2)
52+
53+
class Solution {
54+
public:
55+
56+
57+
int findMinLen(vector<string> &strs){
58+
int n=strs.size();
59+
int minLen=INT_MAX;
60+
for(int i=0;i<n;i++){
61+
if(strs[i].length()<minLen) minLen=strs[i].length();
62+
}
63+
return minLen;
64+
}
65+
66+
string longestCommonPrefix(vector<string>& strs) {
67+
int n=strs.size();
68+
if(n==0) return "";
69+
70+
int minLen=findMinLen(strs);
71+
72+
string res="";
73+
74+
for(int i=0;i<minLen;i++){
75+
char current=strs[0][i];
76+
for(int j=1;j<n;j++){
77+
if(strs[j][i]!=current)
78+
return res;
79+
}
80+
res+=current;
81+
}
82+
83+
return res;
84+
}
85+
};
86+
87+
/*
88+
Suppose you have the input strings as- “geeksforgeeks”, “geeks”, “geek”, “geezer”, “x”
89+
90+
better than word by word
91+
*/
92+
93+
94+
95+
96+
97+
98+
// Sorting O(max* nlogn)
99+
100+
class Solution {
101+
public:
102+
string longestCommonPrefix(vector<string>& strs) {
103+
int n=strs.size();
104+
if(n == 0) return "";
105+
106+
if(n == 1) return strs[0];
107+
108+
// Sort the given array (lexiographically ascending order)
109+
sort(strs.begin(), strs.end());
110+
111+
// Find the minimum length from first and last string
112+
int minLen = min(strs[0].length(), strs[n - 1].length());
113+
114+
// Now the common prefix in first and
115+
// last string is the longest common prefix
116+
string first = strs[0], last = strs[n - 1];
117+
int i = 0;
118+
while (i < minLen && first[i] == last[i])
119+
i++;
120+
121+
return first.substr(0, i);
122+
}
123+
};
124+
125+
126+
127+
128+
// Other Approaches Divide and Conquer, Binary Search, Trie
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
2+
3+
Example 1:
4+
5+
Input: "babad"
6+
Output: "bab"
7+
Note: "aba" is also a valid answer.
8+
Example 2:
9+
10+
Input: "cbbd"
11+
Output: "bb"
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
class Solution {
24+
public:
25+
string longestPalindrome(string s) {
26+
int n=s.length();
27+
28+
vector<vector<bool> >dp(n, vector<bool> (n, 0));
29+
30+
int start=0; // start of lps
31+
int maxLen=1;
32+
33+
// All substrings of length 1 are palindromes
34+
for(int i=0;i<n;i++) dp[i][i]=true;
35+
36+
// check for sub-string of length 2
37+
for(int i=0;i<n-1;i++){
38+
if(s[i]==s[i+1]){
39+
dp[i][i+1]=true;
40+
start=i;
41+
maxLen=2;
42+
}
43+
}
44+
45+
// Check for lengths greater than 2, k is length of substring
46+
for(int k=3;k<=n;k++){
47+
for(int i=0;i<n-k+1;i++){
48+
int j=i+k-1;
49+
if(s[i]==s[j] && dp[i+1][j-1]){
50+
dp[i][j]=true;
51+
start=i;
52+
maxLen=k;
53+
}
54+
}
55+
}
56+
57+
return s.substr(start, maxLen);
58+
}
59+
};
60+
61+
62+
/*
63+
dp[i][j] will be false if substring str[i..j] is not palindrome.
64+
Else dp[i][j] will be true
65+
66+
67+
68+
Time complexity: O(n^2).
69+
Auxiliary Space: O(n^2).
70+
*/
71+
72+
73+
74+
/*
75+
Brute Force Approach
76+
The simple approach is to check each substring whether the substring is a palindrome or not
77+
*/

string/rabin_karp.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// d is the number of characters in the input alphabet
5+
#define d 256
6+
7+
void rabinKarp(string pat, string txt, int q){
8+
int m = pat.length();
9+
int n = txt.length();
10+
int i, j;
11+
int ph = 0; // hash value for pattern
12+
int th = 0; // hash value for txt
13+
int h = 1;
14+
15+
// The value of h would be "pow(d, m-1)%q"
16+
for(i = 0; i < m - 1; i++)
17+
h = (h * d) % q;
18+
19+
// Calculate the hash value of pattern and first window of text
20+
for (i = 0; i < m; i++){
21+
ph = (d * ph + pat[i]) % q;
22+
th = (d * th + txt[i]) % q;
23+
}
24+
25+
// Slide the pattern over text one by one
26+
for (i = 0; i <= n - m; i++){
27+
// Check the hash values of current window of text and pattern.
28+
//If the hash values matches then only we check for characters on by one
29+
if( ph == th ){
30+
// Check for characters one by one
31+
for(j = 0; j < m; j++){
32+
if (txt[i+j] != pat[j])
33+
break;
34+
}
35+
36+
// if ph == th and pat[0...m-1] = txt[i, i+1, ...i+m-1]
37+
if (j == m)
38+
cout<<"Pattern found at index "<< i<<endl;
39+
}
40+
41+
// Calculate hash value for next window of text: removing last digit and adding next digit
42+
th = (d*(th - txt[i]*h) + txt[i+m])%q;
43+
44+
// We might get negative value of t, converting it to positive
45+
if(th < 0)
46+
th = th + q;
47+
}
48+
}
49+
50+
51+
int main(){
52+
string txt = "THIS TEXT IS A RANDOM TEXT";
53+
string pat = "TEXT";
54+
int q = 101; // A prime number
55+
rabinKarp(pat, txt, q);
56+
return 0;
57+
}
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void z_algo(string str, int Z[]){
5+
int n = str.length();
6+
int L, R, idx;
7+
8+
// [L,R] make a window which matches with prefix of s
9+
L = R = 0;
10+
for(int i = 1; i < n; ++i){
11+
// if i>R nothing matches so we will calculate Z[i] using naive way.
12+
if(i > R){
13+
L = R = i;
14+
15+
// R-L = 0 in starting, so it will start checking from 0'th index.
16+
// For example, for "ababab" and i = 1, the value of R
17+
// remains 0 and Z[i] becomes 0.
18+
//For string "aaaaaa" and i = 1, Z[i] and R become 5
19+
while (R<n && str[R-L] == str[R])
20+
R++;
21+
Z[i] = R-L;
22+
R--;
23+
}
24+
else {
25+
// idx = i-L so idx corresponds to number which matches in [L,R] interval
26+
idx = i-L;
27+
28+
// if Z[idx] is less than remaining interval
29+
// then Z[i] will be equal to Z[idx].
30+
// For example, str = "ababab", i = 3, R = 5
31+
// and L = 2
32+
if (i+Z[idx] <= R)
33+
Z[i] = Z[idx];
34+
35+
// For example str = "aaaaaa" and i = 2, R is 5, L is 0
36+
else
37+
{
38+
// else start from R and check manually
39+
L = i;
40+
while (R<n && str[R-L] == str[R])
41+
R++;
42+
Z[i] = R-L;
43+
R--;
44+
}
45+
}
46+
}
47+
}
48+
49+
50+
int main(){
51+
string text = "GEEKS FOR GEEKS";
52+
string pattern = "GEEK";
53+
54+
string str = pattern + "$" + text;
55+
int n = str.length();
56+
57+
int Z[n]; // z array
58+
z_algo(str, Z); // construct z array
59+
60+
for(int i = 0; i < n; ++i){
61+
if (Z[i] == pattern.length())
62+
cout<<"Pattern found at index "<< i - pattern.length() - 1 << endl;
63+
}
64+
return 0;
65+
}

0 commit comments

Comments
 (0)