
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count of Palindromic substrings in an Index range in C++
We are given a string and a range starting from start till end and the task is to calculate the count of palindromic substring present in a given range. Palindrome strings are those strings which are similar from forward and backward of a string like nitin, aba, etc.
For Example
Input - InputString = "cccaabbbdee", start = 2, end = 6;
Output - Count of Palindromic substrings in an Index range 7
Explanation - we are given with a range and a string so, we will start traversing the string from the start pointer which is 2 i.e. 'c' till 6 i.e. 'b' therefore the substring is 'caabb'. So the palindromic substring is 'c', 'a', 'a', 'b' , 'b', 'aa' and 'bb'. So, the count of palindromic substring is 7.
Input - InputString = "lioaabbbdee", start = 0, end = 2;
Output - Count of Palindromic substrings in an Index range 3
Explanation - we are given with a range and a string so, we will start traversing the string from the start pointer which is 0 i.e. 'l' till 2 i.e. 'o' therefore the substring is 'lio'. So the palindromic substring is 'l', 'i' and 'o'. So, the count of palindromic substring is 3.
Approach used in the below program is as follows
- Declare a string of any given size and a range starting from a variable start till end.
- Pass the data to the function named palindrome_index(arr, InputString) for further processing
- Inside the function, declare another array named checked with the array size.
- Start Loop FOR i from 0 till the length of an array
- Inside the loop, start another Loop FOR j from 0 till the length of an array
- Inside the loop, set check as check[i][j] = 0 and arr[i][j] = 0
- Start loop FOR i from length - 1 till i is greater than 0
- Inside the loop, set check and arr of i as 1 then start another loop FOR j from i + 1 till the length of an array
- Inside the loop, check IF string at i i equals to string at j AND i + 1 is greater than j - 1 OR check[i + 1][j - 1]) not equals to 0 then set check[i][j] as 1 ELSE, set check[i][j] as 0 then set arr[i][j] = arr[i][j - 1] + arr[i + 1][j] - arr[i + 1][j - 1] + check[i][j]
- Print a 2-D array as start and end.
Example
import java.io.*; class testqwe { static void palindrome_index(int arr[][], String s) { int length = s.length(); int[][] check = new int[length + 1][length + 1]; for (int i = 0; i <= length; i++) { for (int j = 0; j <= length; j++) { check[i][j] = 0; arr[i][j] = 0; } } for (int i = length - 1; i >= 0; i--) { check[i][i] = arr[i][i] = 1; for (int j = i + 1; j < length; j++) { if(s.charAt(i) == s.charAt(j) && (i + 1 > j - 1 || (check[i + 1][j - 1]) != 0)) { check[i][j] =1; } else { check[i][j] =0; } arr[i][j] = arr[i][j - 1] + arr[i + 1][j] - arr[i + 1][j - 1] + check[i][j]; } } } public static void main(String args[]) { String InputString = "cccaabbbdee"; int[][] arr; arr = new int[50][50]; palindrome_index(arr, InputString); int start = 2; int end = 6; System.out.println("Count of Palindromic substrings in an Index range " + arr[start][end]); } }
If we run the above code it will generate the following output −
Output
Count of Palindromic substrings in an Index range 7
- Related Articles
- Count all Prime Length Palindromic Substrings in C++
- Program to count number of palindromic substrings in Python
- Palindromic Substrings in Python
- Count Binary Substrings in C++
- Rearrange the string to maximize the number of palindromic substrings in C++
- Count of total anagram substrings in C++
- Count of Range Sum in C++
- Find the count of substrings in alphabetic order in C++
- Count all Palindromic Subsequence in a given String in C++
- Count of index pairs with equal elements in an array in C++
- Count number of substrings with exactly k distinct characters in C++
- Count of substrings of a binary string containing K ones in C++
- Count Unique Characters of All Substrings of a Given String in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Maximum count of substrings of length K consisting of same characters in C++
