Count All Palindromic Subsequence in a given String
Last Updated :
01 Oct, 2024
Given a string s of length n, the task is to count number of palindromic subsequence (need not necessarily be distinct) present in the string s.
Example:
Input: s = "abcd"
Output: 4
Explanation: Palindromic subsequence are : "a" ,"b", "c" ,"d"
Input: s = "aab"
Output: 4
Explanation: palindromic subsequence are :"a", "a", "b", "aa"
Input: s = "geeksforgeeks"
Output: 81
Naive Recursive Approach
A very naive approach would be to generate all possible subsequences of the string and check each one to see if it's a palindrome. This approach is highly inefficient due to the exponential number of subsequences.
Steps-by-step approach:
- Generate all subsequences of the string.
- For each subsequence, check if it's a palindrome.
- Count all subsequences that are palindromes.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string &s) {
int left = 0, right = s.size() - 1;
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}
// Recursive function to generate subsequences
// and count palindromic ones
int generateSubseq(string &s, int i, string curr) {
// Base case: if we've considered all characters in the string
if (i == s.size()) {
// Check if the current substring is a palindrome
// and return 1 if it is, else return 0
return isPalindrome(curr) && curr.size() > 0 ? 1 : 0;
}
// Include the current character in the subsequence
int res1 = generateSubseq(s, i + 1, curr + s[i]);
// Exclude the current character from the subsequence
int res2 = generateSubseq(s, i + 1, curr);
// Return the total count of palindromic subsequences found
return res1 + res2;
}
// Function to count the number of palindromic subsequences
// in a given string
int countPS(string s) {
return generateSubseq(s, 0, "");
}
int main() {
string s = "geeksforgeeks";
cout << countPS(s);
}
C
#include <stdio.h>
#include <string.h>
// Function to check if a string is a palindrome
int isPalindrome(char *s) {
int left = 0, right = strlen(s) - 1;
while (left < right) {
if (s[left] != s[right]) {
return 0;
}
left++;
right--;
}
return 1;
}
// Recursive function to generate subsequences
// and count palindromic ones
int generateSubseq(char *s, int i, char *curr) {
// Base case: if we've considered all characters
if (s[i] == '\0') {
return isPalindrome(curr) && curr[0] != '\0' ? 1 : 0;
}
// Include the current character in the subsequence
char newCurr[100];
strcpy(newCurr, curr);
strncat(newCurr, &s[i], 1);
int res1 = generateSubseq(s, i + 1, newCurr);
// Exclude the current character from the subsequence
int res2 = generateSubseq(s, i + 1, curr);
// Return the total count of palindromic subsequences found
return res1 + res2;
}
// Function to count the number of palindromic subsequences
// in a given string
int countPS(char *s) {
char curr[100] = "";
return generateSubseq(s, 0, curr);
}
int main() {
char s[] = "geeksforgeeks";
printf("%d\n", countPS(s));
return 0;
}
Java
class GfG {
// Function to check if a string is a palindrome
static boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
// Recursive function to generate subsequences
// and count palindromic ones
static int generateSubseq(String s, int i, String curr) {
// Base case: if we've considered all characters
if (i == s.length()) {
return isPalindrome(curr) && curr.length() > 0 ? 1 : 0;
}
// Include the current character in the subsequence
int res1 = generateSubseq(s, i + 1, curr + s.charAt(i));
// Exclude the current character from the subsequence
int res2 = generateSubseq(s, i + 1, curr);
// Return the total count of palindromic subsequences found
return res1 + res2;
}
// Function to count the number of palindromic subsequences
// in a given string
static int countPS(String s) {
return generateSubseq(s, 0, "");
}
public static void main(String[] args) {
String s = "geeksforgeeks";
System.out.println(countPS(s));
}
}
Python
# Function to check if a string is a palindrome
def isPalindrome(s):
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
# Recursive function to generate subsequences
# and count palindromic ones
def generateSubseq(s, i, curr):
# Base case: if we've considered all characters
if i == len(s):
return 1 if isPalindrome(curr) and curr else 0
# Include the current character in the subsequence
res1 = generateSubseq(s, i + 1, curr + s[i])
# Exclude the current character from the subsequence
res2 = generateSubseq(s, i + 1, curr)
# Return the total count of palindromic subsequences found
return res1 + res2
# Function to count the number of palindromic subsequences
# in a given string
def countPS(s):
return generateSubseq(s, 0, "")
# Driver code
s = "geeksforgeeks"
print(countPS(s))
C#
using System;
class GfG {
// Function to check if a string is a palindrome
static bool IsPalindrome(string s) {
int left = 0, right = s.Length - 1;
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}
// Recursive function to generate subsequences
// and count palindromic ones
static int GenerateSubseq(string s, int i, string curr) {
// Base case: if we've considered all characters
if (i == s.Length) {
return IsPalindrome(curr) && curr.Length > 0 ? 1 : 0;
}
// Include the current character in the subsequence
int res1 = GenerateSubseq(s, i + 1, curr + s[i]);
// Exclude the current character from the subsequence
int res2 = GenerateSubseq(s, i + 1, curr);
// Return the total count of palindromic subsequences found
return res1 + res2;
}
// Function to count the number of palindromic subsequences
// in a given string
static int CountPS(string s) {
return GenerateSubseq(s, 0, "");
}
static void Main() {
string s = "geeksforgeeks";
Console.WriteLine(CountPS(s));
}
}
JavaScript
// Function to check if a string is a palindrome
function isPalindrome(s) {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) {
return false;
}
left++;
right--;
}
return true;
}
// Recursive function to generate subsequences
// and count palindromic ones
function generateSubseq(s, i, curr) {
// Base case: if we've considered all characters
if (i === s.length) {
return isPalindrome(curr) && curr.length > 0 ? 1 : 0;
}
// Include the current character in the subsequence
let res1 = generateSubseq(s, i + 1, curr + s[i]);
// Exclude the current character from the subsequence
let res2 = generateSubseq(s, i + 1, curr);
// Return the total count of palindromic subsequences found
return res1 + res2;
}
// Function to count the number of palindromic subsequences
// in a given string
function countPS(s) {
return generateSubseq(s, 0, "");
}
// Driver code
let s = "geeksforgeeks";
console.log(countPS(s));
Time Complexity: O(n*2n), where n is the length of the string. This is because there are 2n subsequences and checking each one takes O(n).
Auxiliary Space: O(n), The recursion stack can go as deep as the length of the string n.
Memoization Approach
In the above naive approach we've generated all possible subsequences, which is inefficient due to the exponential number of combinations. We can use Memoization techique to optimize this process by storing results of computed subproblem so that we don’t need to recalculate the result for same subproblem, if it occurs.
When looking at the characters at the start and end of a substring:
- If the characters are the same, it means we can form palindromes that include both characters.
- If the characters are different, we need to count palindromes by ignoring one character at a time. This will helps us find all possible palindromic subsequences without missing any.
Recurrance Relation:
- If s[i] == s[j], the number of palindromic subsequences is: count(i, j) = 1 + count(i + 1, j) + count(i, j - 1).
The "+1" counts the new palindrome formed by s[i] and s[j]. - If s[i] != s[j], the relation becomes: count(i, j) = count(i + 1, j) + count(i, j - 1) - count(i + 1,j - 1)
We subtract count(i + 1, j - 1) because it is counted twice.
C++
#include <bits/stdc++.h>
using namespace std;
// Recursive utility function to count palindromic subsequences
// in the substring s[i..j] using memoization to store results
int countPSUtil(string &s, int i, int j, vector<vector<int>> &memo){
// Base case: if the starting index exceeds the ending index
if (i > j)
return 0;
// Base case: if there is only one character, it's a palindrome
if (i == j)
return 1;
// Return the already computed subproblem if it exists
if (memo[i][j] != -1)
return memo[i][j];
if (s[i] == s[j]){
// Count palindromes by including both characters
// and counting palindromes in the remaining substrings
memo[i][j] = 1 + countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo);
}
else{
// If characters are different, count palindromes by excluding
// one character from either end and subtracting the overlap
memo[i][j] = countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo) -
countPSUtil(s, i + 1, j - 1, memo);
}
// Return the computed result for the substring s[i..j]
return memo[i][j];
}
// Function to count the number of palindromic subsequences
// in a given string
int countPS(string s){
int n = s.size();
// Create a memoization table initialized to -1
vector<vector<int>> memo(n, vector<int>(n, -1));
return countPSUtil(s, 0, n - 1, memo);
}
int main(){
string s = "geeksforgeeks";
cout << countPS(s);
}
C
#include <stdio.h>
#include <string.h>
// Function to count palindromic subsequences
// in the substring s[i..j] using memoization
int countPSUtil(char *s, int i, int j, int memo[][100]) {
// Base case: if the starting index exceeds the ending index
if (i > j)
return 0;
// Base case: if there is only one character
if (i == j)
return 1;
// Return the already computed subproblem if it exists
if (memo[i][j] != -1)
return memo[i][j];
if (s[i] == s[j]) {
// Count palindromes by including both characters
memo[i][j] = 1 + countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo);
} else {
// If characters are different, count palindromes
memo[i][j] = countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo) -
countPSUtil(s, i + 1, j - 1, memo);
}
// Return the computed result for the substring s[i..j]
return memo[i][j];
}
// Function to count the number of palindromic subsequences
// in a given string
int countPS(char *s) {
int n = strlen(s);
int memo[100][100];
// Initialize memoization table to -1
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
memo[i][j] = -1;
return countPSUtil(s, 0, n - 1, memo);
}
int main() {
char s[] = "geeksforgeeks";
printf("%d\n", countPS(s));
return 0;
}
Java
class GfG {
// Recursive utility function to count palindromic subsequences
// in the substring s[i..j] using memoization
static int countPSUtil(String s, int i, int j, int[][] memo) {
// Base case: if the starting index exceeds the ending index
if (i > j)
return 0;
// Base case: if there is only one character
if (i == j)
return 1;
// Return the already computed subproblem if it exists
if (memo[i][j] != -1)
return memo[i][j];
if (s.charAt(i) == s.charAt(j)) {
// Count palindromes by including both characters
memo[i][j] = 1 + countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo);
} else {
// If characters are different, count palindromes
memo[i][j] = countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo) -
countPSUtil(s, i + 1, j - 1, memo);
}
// Return the computed result for the substring s[i..j]
return memo[i][j];
}
// Function to count the number of palindromic subsequences
// in a given string
static int countPS(String s) {
int n = s.length();
int[][] memo = new int[n][n];
// Initialize memoization table to -1
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
memo[i][j] = -1;
return countPSUtil(s, 0, n - 1, memo);
}
public static void main(String[] args) {
String s = "geeksforgeeks";
System.out.println(countPS(s));
}
}
Python
# Recursive utility function to count palindromic subsequences
# in the substring s[i..j] using memoization
def countPSUtil(s, i, j, memo):
# Base case: if the starting index exceeds the ending index
if i > j:
return 0
# Base case: if there is only one character
if i == j:
return 1
# Return the already computed subproblem if it exists
if memo[i][j] != -1:
return memo[i][j]
if s[i] == s[j]:
# Count palindromes by including both characters
memo[i][j] = 1 + countPSUtil(s, i + 1, j, memo) + \
countPSUtil(s, i, j - 1, memo)
else:
# If characters are different, count palindromes
memo[i][j] = countPSUtil(s, i + 1, j, memo) + \
countPSUtil(s, i, j - 1, memo) - \
countPSUtil(s, i + 1, j - 1, memo)
# Return the computed result for the substring s[i..j]
return memo[i][j]
# Function to count the number of palindromic subsequences
# in a given string
def countPS(s):
n = len(s)
memo = [[-1] * n for _ in range(n)]
return countPSUtil(s, 0, n - 1, memo)
# Driver code
s = "geeksforgeeks"
print(countPS(s))
C#
using System;
class GfG {
// Recursive utility function to count palindromic subsequences
// in the substring s[i..j] using memoization
static int CountPSUtil(string s, int i, int j, int[,] memo) {
// Base case: if the starting index exceeds the ending index
if (i > j)
return 0;
// Base case: if there is only one character
if (i == j)
return 1;
// Return the already computed subproblem if it exists
if (memo[i, j] != -1)
return memo[i, j];
if (s[i] == s[j]) {
// Count palindromes by including both characters
memo[i, j] = 1 + CountPSUtil(s, i + 1, j, memo) +
CountPSUtil(s, i, j - 1, memo);
} else {
// If characters are different, count palindromes
memo[i, j] = CountPSUtil(s, i + 1, j, memo) +
CountPSUtil(s, i, j - 1, memo) -
CountPSUtil(s, i + 1, j - 1, memo);
}
// Return the computed result for the substring s[i..j]
return memo[i, j];
}
// Function to count the number of palindromic subsequences
// in a given string
static int CountPS(string s) {
int n = s.Length;
int[,] memo = new int[n, n];
// Initialize memoization table to -1
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
memo[i, j] = -1;
return CountPSUtil(s, 0, n - 1, memo);
}
static void Main() {
string s = "geeksforgeeks";
Console.WriteLine(CountPS(s));
}
}
JavaScript
// Recursive utility function to count palindromic subsequences
// in the substring s[i..j] using memoization
function countPSUtil(s, i, j, memo) {
// Base case: if the starting index exceeds the ending index
if (i > j) return 0;
// Base case: if there is only one character
if (i === j) return 1;
// Return the already computed subproblem if it exists
if (memo[i][j] !== -1) return memo[i][j];
if (s[i] === s[j]) {
// Count palindromes by including both characters
memo[i][j] = 1 + countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo);
} else {
// If characters are different, count palindromes
memo[i][j] = countPSUtil(s, i + 1, j, memo) +
countPSUtil(s, i, j - 1, memo) -
countPSUtil(s, i + 1, j - 1, memo);
}
// Return the computed result for the substring s[i..j]
return memo[i][j];
}
// Function to count the number of palindromic subsequences
// in a given string
function countPS(s) {
const n = s.length;
const memo = Array.from(Array(n), () => Array(n).fill(-1));
return countPSUtil(s, 0, n - 1, memo);
}
// Driver code
const s = "geeksforgeeks";
console.log(countPS(s));
Time Complexity: O(n2), The recursive function explores all pairs of indices, leading to a O(n2) number of subproblems.
Auxiliary Space: O(n2), A 2D memoization array of size n*n is used to store results of subproblems.
Dynamic Programming Approach (Buttom-up/Tabulation)
This is the most optimized approach where we use a 2D dynamic programming table to store the number of palindromic subsequences for every substring of the given string. This avoids the overhead of recursive calls and memoization.
Recurrance Relation:
Let dp[i][j] be the number of palindromic subsequences in the substring s[i...j]. The relation is the same as described in the memoized approach:
- If s[i] == s[j]: dp[i][j] = dp[i + 1][j] + dp[i][j-1] + 1
- If s[i] != s[j]: dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1]
C++
#include <bits/stdc++.h>
using namespace std;
int countPS(string &s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
// Every single character is a palindrome,
// so initialize diagonal elements
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
// Fill the table for substrings of length greater than 1
for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
if (s[i] == s[j]) {
dp[i][j] = dp[i+1][j] + dp[i][j-1] + 1;
} else {
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];
}
}
}
return dp[0][n-1];
}
int main(){
string s = "geeksforgeeks";
cout << countPS(s);
}
C
#include <stdio.h>
#include <string.h>
int countPS(char *s) {
int n = strlen(s);
int dp[100][100] = {0};
// Every single character is a palindrome
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
// Fill the table for substrings of length greater than 1
for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
if (s[i] == s[j]) {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] + 1;
} else {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1]
- dp[i + 1][j - 1];
}
}
}
return dp[0][n - 1];
}
int main() {
char s[] = "geeksforgeeks";
printf("%d\n", countPS(s));
return 0;
}
Java
class GfG {
static int countPS(String s) {
int n = s.length();
int[][] dp = new int[n][n];
// Every single character is a palindrome
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
// Fill the table for substrings of length greater than 1
for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] + 1;
} else {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1]
- dp[i + 1][j - 1];
}
}
}
return dp[0][n - 1];
}
public static void main(String[] args) {
String s = "geeksforgeeks";
System.out.println(countPS(s));
}
}
Python
def countPS(s):
n = len(s)
dp = [[0] * n for _ in range(n)]
# Every single character is a palindrome
for i in range(n):
dp[i][i] = 1
# Fill the table for substrings of length greater than 1
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
if s[i] == s[j]:
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] + 1
else:
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1]
return dp[0][n - 1]
# Driver code
s = "geeksforgeeks"
print(countPS(s))
C#
using System;
class GfG {
static int CountPS(string s) {
int n = s.Length;
int[,] dp = new int[n, n];
// Every single character is a palindrome
for (int i = 0; i < n; i++) {
dp[i, i] = 1;
}
// Fill the table for substrings of length greater than 1
for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
if (s[i] == s[j]) {
dp[i, j] = dp[i + 1, j] + dp[i, j - 1] + 1;
} else {
dp[i, j] = dp[i + 1, j] + dp[i, j - 1] - dp[i + 1, j - 1];
}
}
}
return dp[0, n - 1];
}
static void Main() {
string s = "geeksforgeeks";
Console.WriteLine(CountPS(s));
}
}
JavaScript
function countPS(s) {
const n = s.length;
const dp = Array.from(Array(n), () => Array(n).fill(0));
// Every single character is a palindrome
for (let i = 0; i < n; i++) {
dp[i][i] = 1;
}
// Fill the table for substrings of length greater than 1
for (let length = 2; length <= n; length++) {
for (let i = 0; i <= n - length; i++) {
const j = i + length - 1;
if (s[i] === s[j]) {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] + 1;
} else {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
}
}
}
return dp[0][n - 1];
}
// Driver code
const s = "geeksforgeeks";
console.log(countPS(s));
Time Complexity: O(n2), Filling the DP table involves two nested loops, each running up to n.
Auxiliary Space: O(n2), A 2D DP array of size n*n is used to store results.
Count Palindromic Subsequences | DSA Problem
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