Given two strings 's1' and 's2', find the length of the longest common substring.
Example:
Input: s1 = "GeeksforGeeks", s2 = "GeeksQuiz"
Output : 5
Explanation:
The longest common substring is "Geeks" and is of length 5.
Input: s1 = "abcdxyz", s2 = "xyzabcd"
Output : 4
Explanation:
The longest common substring is "abcd" and is of length 4.
Input: s1 = "abc", s2 = ""
Output : 0.
Naive Iterative Method
A simple solution is to try all substrings beginning with every pair of index from s1 and s2 and keep track of the longest matching substring. We run nested loops to generate all pairs of indexes and then inside this nested loop, we try all lengths. We find the maximum length beginning with every pair. And finally take the max of all maximums.
C++
#include <bits/stdc++.h>
using namespace std;
int maxCommStr(string & s1, string& s2) {
int m = s1.length();
int n = s2.length();
// Consider every pair of index and find the length
// of the longest common substring beginning with
// every pair. Finally return max of all maximums.
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int curr = 0;
while ((i + curr) < m && (j + curr) < n
&& s1[i + curr] == s2[j + curr]) {
curr++;
}
res = max(res, curr);
}
}
return res;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "practicewritegeekscourses";
cout << maxCommStr(s1, s2) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
int maxCommStr(char* s1, char* s2) {
int m = strlen(s1);
int n = strlen(s2);
int res = 0;
// Consider every pair of index and find the length
// of the longest common substring beginning with
// every pair. Finally return max of all maximums.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int curr = 0;
while ((i + curr) < m && (j + curr) < n
&& s1[i + curr] == s2[j + curr]) {
curr++;
}
if (curr > res) {
res = curr;
}
}
}
return res;
}
int main() {
char s1[] = "geeksforgeeks";
char s2[] = "practicewritegeekscourses";
printf("%d\n", maxCommStr(s1, s2));
return 0;
}
Java
class GfG {
// Function to find the length of the longest common substring
static int maxCommStr(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int res = 0;
// Consider every pair of index and find the length
// of the longest common substring beginning with
// every pair. Finally return max of all maximums.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int curr = 0;
while ((i + curr) < m && (j + curr) < n
&& s1.charAt(i + curr) == s2.charAt(j + curr)) {
curr++;
}
res = Math.max(res, curr);
}
}
return res;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "practicewritegeekscourses";
System.out.println(maxCommStr(s1, s2));
}
}
Python
def maxCommStr(s1, s2):
m = len(s1)
n = len(s2)
res = 0
# Consider every pair of index and find the length
# of the longest common substring beginning with
# every pair. Finally return max of all maximums.
for i in range(m):
for j in range(n):
curr = 0
while (i + curr) < m and (j + curr) < n and s1[i + curr] == s2[j + curr]:
curr += 1
res = max(res, curr)
return res
s1 = "geeksforgeeks"
s2 = "practicewritegeekscourses"
print(maxCommStr(s1, s2))
C#
using System;
class GfG {
static int MaxCommStr(string s1, string s2) {
int m = s1.Length;
int n = s2.Length;
int res = 0;
// Consider every pair of index and find the length
// of the longest common substring beginning with
// every pair. Finally return max of all maximums.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int curr = 0;
while ((i + curr) < m && (j + curr) < n
&& s1[i + curr] == s2[j + curr]) {
curr++;
}
res = Math.Max(res, curr);
}
}
return res;
}
static void Main() {
string s1 = "geeksforgeeks";
string s2 = "practicewritegeekscourses";
Console.WriteLine(MaxCommStr(s1, s2));
}
}
JavaScript
function maxCommStr(s1, s2) {
let m = s1.length;
let n = s2.length;
let res = 0;
// Consider every pair of index and find the length
// of the longest common substring beginning with
// every pair. Finally return max of all maximums.
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let curr = 0;
while ((i + curr) < m && (j + curr) < n &&
s1[i + curr] === s2[j + curr]) {
curr++;
}
res = Math.max(res, curr);
}
}
return res;
}
let s1 = "geeksforgeeks";
let s2 = "practicewritegeekscourses";
console.log(maxCommStr(s1, s2));
Time Complexity: O(m x n x min(m, n))
Auxiliary Space: O(1)
Naive Recursive Method
Below is a recursive version of the above solution, we write a recursive method to find the maximum length substring ending with given pair of indexes. We mainly find longest common suffix. We can solve by finding longest common prefix for every pair (like we did in the above iterative solution). We call this recursive function inside nested loops to get the overall maximum.
C++
#include <bits/stdc++.h>
using namespace std;
// Returns length of the longest common substring
// ending with the last characters. We mainly find
// Longest common suffix.
int LCSuf(const string& s1, const string& s2, int m, int n) {
if (m == 0 || n == 0 || s1[m - 1] != s2[n - 1])
return 0;
return 1 + LCSuf(s1, s2, m - 1, n - 1);
}
int maxCommStr(const string& s1, const string& s2) {
int res = 0;
// Find the longest common substring ending
// at every pair of characters and take the
// maximum of all.
for (int i = 1; i <= s1.size(); i++) {
for (int j = 1; j <= s2.size(); j++) {
res = max(res, LCSuf(s1, s2, i, j));
}
}
return res;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "practicewritegeekscourses";
cout << maxCommStr(s1, s2) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Returns length of the longest common substring
// ending with the last characters. We mainly find
// Longest common suffix.
int LCSuf(const char* s1, const char* s2, int m, int n) {
if (m == 0 || n == 0 || s1[m - 1] != s2[n - 1])
return 0;
return 1 + LCSuf(s1, s2, m - 1, n - 1);
}
int maxCommStr(const char* s1, const char* s2) {
int res = 0;
int m = strlen(s1);
int n = strlen(s2);
// Find the longest common substring ending
// at every pair of characters and take the
// maximum of all.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
res = res > LCSuf(s1, s2, i, j) ? res : LCSuf(s1, s2, i, j);
}
}
return res;
}
int main() {
char s1[] = "geeksforgeeks";
char s2[] = "practicewritegeekscourses";
printf("%d\n", maxCommStr(s1, s2));
return 0;
}
Java
class GfG {
// Returns length of the longest common substring
// ending with the last characters. We mainly find
// Longest common suffix.
static int LCSuf(String s1, String s2, int m, int n) {
if (m == 0 || n == 0 || s1.charAt(m - 1)
!= s2.charAt(n - 1)) {
return 0;
}
return 1 + LCSuf(s1, s2, m - 1, n - 1);
}
static int maxCommStr(String s1, String s2) {
int res = 0;
int m = s1.length();
int n = s2.length();
// Find the longest common substring ending
// at every pair of characters and take the
// maximum of all.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
res = Math.max(res, LCSuf(s1, s2, i, j));
}
}
return res;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "practicewritegeekscourses";
System.out.println(maxCommStr(s1, s2));
}
}
Python
def LCSuf(s1, s2, m, n):
if m == 0 or n == 0 or s1[m - 1] != s2[n - 1]:
return 0
return 1 + LCSuf(s1, s2, m - 1, n - 1)
def maxCommStr(s1, s2):
res = 0
m = len(s1)
n = len(s2)
# Find the longest common substring ending
# at every pair of characters and take the
# maximum of all.
for i in range(1, m + 1):
for j in range(1, n + 1):
res = max(res, LCSuf(s1, s2, i, j))
return res
s1 = "geeksforgeeks"
s2 = "practicewritegeekscourses"
print(maxCommStr(s1, s2))
C#
using System;
class GfG {
// Returns length of the longest common substring
// ending with the last characters. We mainly find
// Longest common suffix.
static int LCSuf(string s1, string s2, int m, int n) {
if (m == 0 || n == 0 || s1[m - 1] != s2[n - 1]) {
return 0;
}
return 1 + LCSuf(s1, s2, m - 1, n - 1);
}
static int maxCommStr(string s1, string s2) {
int res = 0;
int m = s1.Length;
int n = s2.Length;
// Find the longest common substring ending
// at every pair of characters and take the
// maximum of all.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
res = Math.Max(res, LCSuf(s1, s2, i, j));
}
}
return res;
}
static void Main() {
string s1 = "geeksforgeeks";
string s2 = "practicewritegeekscourses";
Console.WriteLine(maxCommStr(s1, s2));
}
}
JavaScript
function LCSuf(s1, s2, m, n) {
if (m === 0 || n === 0 || s1[m - 1] !== s2[n - 1]) {
return 0;
}
return 1 + LCSuf(s1, s2, m - 1, n - 1);
}
function maxCommStr(s1, s2) {
let res = 0;
let m = s1.length;
let n = s2.length;
// Find the longest common substring ending
// at every pair of characters and take the
// maximum of all.
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
res = Math.max(res, LCSuf(s1, s2, i, j));
}
}
return res;
}
let s1 = "geeksforgeeks";
let s2 = "practicewritegeekscourses";
console.log(maxCommStr(s1, s2));
Time Complexity: O(m x n x min(m, n))
Auxiliary Space: O(min(m, n))
Dynamic Programming Solution
If we take a look at the above recursive solution, we can notice that there are overlapping subproblems. We call lcsEnding() for same pair of lengths. With the help of DP, we can optimize the solution to O(m x n)
The longest common suffix has following optimal substructure property
If last characters match, then we reduce both lengths by 1
- LCSuff(s1, s2, m, n) = LCSuff(s1, s2, m-1, n-1) + 1 if s1[m-1] = s2[n-1]
If last characters do not match, then result is 0, i.e.,
- LCSuff(s1, s2, m, n) = 0 if (s1[m-1] != s2[n-1])
Now ,we consider suffixes of different substrings ending at different indexes.
The maximum length Longest Common Suffix is the longest common substring.
LCSubStr(s1, s2, m, n) = Max(LCSuff(s1, s2, i, j)) where 1 <= i <= m and 1 <= j <= n
C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Returns length of longest common substring of
// s1[0..m-1] and s2[0..n-1]
int maxCommStr(const string& s1, const string& s2) {
int m = s1.length();
int n = s2.length();
// Create a table to store lengths of longest
// common suffixes of substrings.
vector<vector<int>> LCSuf(m + 1, vector<int>(n + 1, 0));
// Build LCSuf[m+1][n+1] in bottom-up fashion.
int res = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
LCSuf[i][j] = LCSuf[i - 1][j - 1] + 1;
res = max(res, LCSuf[i][j]);
} else {
LCSuf[i][j] = 0;
}
}
}
return res;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "ggeegeeksquizpractice";
cout << maxCommStr(s1, s2) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
// Returns length of longest common substring of
// s1[0..m-1] and s2[0..n-1]
int maxCommStr(const char* s1, const char* s2) {
int m = strlen(s1);
int n = strlen(s2);
// Create a table to store lengths of longest
// common suffixes of substrings.
int LCSuf[m + 1][n + 1];
// Build LCSuf[m+1][n+1] in bottom-up fashion.
int res = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
LCSuf[i][j] = LCSuf[i - 1][j - 1] + 1;
if (LCSuf[i][j] > res) {
res = LCSuf[i][j];
}
} else {
LCSuf[i][j] = 0;
}
}
}
return res;
}
int main() {
char s1[] = "geeksforgeeks";
char s2[] = "ggeegeeksquizpractice";
printf("%d\n", maxCommStr(s1, s2));
return 0;
}
Java
public class Main {
// Returns length of longest common substring of
// s1[0..m-1] and s2[0..n-1]
public static int maxCommStr(String s1, String s2) {
int m = s1.length();
int n = s2.length();
// Create a table to store lengths of longest
// common suffixes of substrings. Note that LCSuf[i][j]
// is going to contain length of longest common suffix
// of s1[0..i-1] and s2[0..j-1].
int[][] LCSuf = new int[m + 1][n + 1];
int res = 0;
// Following steps build LCSuf[m+1][n+1] in bottom up fashion.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
LCSuf[i][j] = LCSuf[i - 1][j - 1] + 1;
res = Math.max(res, LCSuf[i][j]);
} else {
LCSuf[i][j] = 0;
}
}
}
return res;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "ggeegeeksquizpractice";
System.out.println(maxCommStr(s1, s2));
}
}
Python
def maxCommStr(s1, s2):
m = len(s1)
n = len(s2)
# Create a table to store lengths of longest
# common suffixes of substrings.
LCSuf = [[0] * (n + 1) for _ in range(m + 1)]
res = 0
# Build LCSuf[m+1][n+1] in bottom-up fashion.
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
LCSuf[i][j] = LCSuf[i - 1][j - 1] + 1
res = max(res, LCSuf[i][j])
else:
LCSuf[i][j] = 0
return res
s1 = "geeksforgeeks"
s2 = "ggeegeeksquizpractice"
print(maxCommStr(s1, s2))
C#
using System;
public class MainClass {
public static int maxCommStr(string s1, string s2) {
int m = s1.Length;
int n = s2.Length;
// Create a table to store lengths of longest
// common suffixes of substrings.
int[,] LCSuf = new int[m + 1, n + 1];
int res = 0;
// Build LCSuf[m+1][n+1] in bottom-up fashion.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
LCSuf[i, j] = LCSuf[i - 1, j - 1] + 1;
res = Math.Max(res, LCSuf[i, j]);
} else {
LCSuf[i, j] = 0;
}
}
}
return res;
}
public static void Main(string[] args) {
string s1 = "geeksforgeeks";
string s2 = "ggeegeeksquizpractice";
Console.WriteLine(maxCommStr(s1, s2));
}
}
JavaScript
function maxCommStr(s1, s2) {
let m = s1.length;
let n = s2.length;
// Create a table to store lengths of longest
// common suffixes of substrings.
let LCSuf = Array.from(Array(m + 1), () => Array(n + 1).fill(0));
let res = 0;
// Build LCSuf[m+1][n+1] in bottom-up fashion.
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (s1[i - 1] === s2[j - 1]) {
LCSuf[i][j] = LCSuf[i - 1][j - 1] + 1;
res = Math.max(res, LCSuf[i][j]);
} else {
LCSuf[i][j] = 0;
}
}
}
return res;
}
let s1 = "geeksforgeeks";
let s2 = "ggeegeeksquizpractice";
console.log(maxCommStr(s1, s2));
Time Complexity: O(m*n)
Auxiliary Space: O(m*n), since m*n extra space has been taken.
Space Optimized DP
In the above approach, we are only using the last row of the 2-D array. Hence we can optimize the space by using a 1-D array.
C++
#include <iostream>
#include <vector>
using namespace std;
// Function to find the length of the longest LCS
// with space optimization
int longestCommonSubstr(const string& s1, const string& s2) {
int m = s1.length();
int n = s2.length();
// Create a 1D array to store the previous row's results
vector<int> prev(n + 1, 0);
int res = 0;
for (int i = 1; i <= m; i++) {
// Create a temporary array to store the current row
vector<int> curr(n + 1, 0);
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
curr[j] = prev[j - 1] + 1;
res = max(res, curr[j]);
} else {
curr[j] = 0;
}
}
// Move the current row's data to the previous row
prev = curr;
}
return res;
}
// Driver Code
int main() {
string s1 = "geeksforgeeks";
string s2 = "ggeegeeksquizpractice";
cout << longestCommonSubstr(s1, s2) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
// Function to find the length of the longest LCS
// with space optimization
int longestCommonSubstr(const char* s1, const char* s2) {
int m = strlen(s1);
int n = strlen(s2);
// Create a 1D array to store the previous row's results
int prev[n + 1];
memset(prev, 0, sizeof(prev));
int res = 0;
for (int i = 1; i <= m; i++) {
// Create a temporary array to store the current row
int curr[n + 1];
memset(curr, 0, sizeof(curr));
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
curr[j] = prev[j - 1] + 1;
if (curr[j] > res) {
res = curr[j];
}
} else {
curr[j] = 0;
}
}
// Move the current row's data to the previous row
memcpy(prev, curr, sizeof(curr));
}
return res;
}
int main() {
char s1[] = "geeksforgeeks";
char s2[] = "ggeegeeksquizpractice";
printf("%d\n", longestCommonSubstr(s1, s2));
return 0;
}
Java
public class GfG {
// Function to find the length of the longest LCS
// with space optimization
public static int longestCommonSubstr(String s1, String s2) {
int m = s1.length();
int n = s2.length();
// Create a 1D array to store the previous row's results
int[] prev = new int[n + 1];
int res = 0;
for (int i = 1; i <= m; i++) {
// Create a temporary array to store the current row
int[] curr = new int[n + 1];
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
curr[j] = prev[j - 1] + 1;
res = Math.max(res, curr[j]);
} else {
curr[j] = 0;
}
}
// Move the current row's data to the previous row
prev = curr;
}
return res;
}
public static void main(String[] args) {
String s1 = "geeksforgeeks";
String s2 = "ggeegeeksquizpractice";
System.out.println(longestCommonSubstr(s1, s2));
}
}
Python
def longestCommonSubstr(s1, s2):
m = len(s1)
n = len(s2)
# Create a 1D array to store the previous row's results
prev = [0] * (n + 1)
res = 0
for i in range(1, m + 1):
# Create a temporary array to store the current row
curr = [0] * (n + 1)
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
curr[j] = prev[j - 1] + 1
res = max(res, curr[j])
else:
curr[j] = 0
# Move the current row's data to the previous row
prev = curr
return res
# Driver Code
s1 = "geeksforgeeks"
s2 = "ggeegeeksquizpractice"
print(longestCommonSubstr(s1, s2))
C#
using System;
public class MainClass {
// Function to find the length of the longest LCS
// with space optimization
public static int longestCommonSubstr(string s1, string s2) {
int m = s1.Length;
int n = s2.Length;
// Create a 1D array to store the previous row's results
int[] prev = new int[n + 1];
int res = 0;
for (int i = 1; i <= m; i++) {
// Create a temporary array to store the current row
int[] curr = new int[n + 1];
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
curr[j] = prev[j - 1] + 1;
res = Math.Max(res, curr[j]);
} else {
curr[j] = 0;
}
}
// Move the current row's data to the previous row
prev = curr;
}
return res;
}
public static void Main(string[] args) {
string s1 = "geeksforgeeks";
string s2 = "ggeegeeksquizpractice";
Console.WriteLine(longestCommonSubstr(s1, s2));
}
}
JavaScript
function longestCommonSubstr(s1, s2) {
let m = s1.length;
let n = s2.length;
// Create a 1D array to store the previous row's results
let prev = new Array(n + 1).fill(0);
let res = 0;
for (let i = 1; i <= m; i++) {
// Create a temporary array to store the current row
let curr = new Array(n + 1).fill(0);
for (let j = 1; j <= n; j++) {
if (s1[i - 1] === s2[j - 1]) {
curr[j] = prev[j - 1] + 1;
res = Math.max(res, curr[j]);
} else {
curr[j] = 0;
}
}
// Move the current row's data to the previous row
prev = curr;
}
return res;
}
// Driver Code
let s1 = "geeksforgeeks";
let s2 = "ggeegeeksquizpractice";
console.log(longestCommonSubstr(s1, s2));
Time Complexity: O(m * n)
Auxiliary Space: O(n)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem