find all the palindrome substring in a given string
C++
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
// expand in both directions of low and high to find all palindromes
void expand(string str, int low, int high, auto &set)
{
// run till str[low.high] is a palindrome
while (low >= 0 && high < str.length()
&& str[low] == str[high])
{
// push all palindromes into the set
set.insert(str.substr(low, high - low + 1));
// expand in both directions
low--, high++;
}
}
// Function to find all unique palindromic substrings of given string
void allPalindromicSubstrings(string str)
{
// create an empty set to store all unique palindromic substrings
unordered_set<string> set;
for (int i = 0; i < str.length(); i++)
{
// find all odd length palindrome with str[i] as mid point
expand(str, i, i, set);
// find all even length palindrome with str[i] and str[i+1] as
// its mid points
expand(str, i, i + 1, set);
}
// print all unique palindromic substrings
for (auto i : set)
cout << i << " ";
}
int main()
{
string str = "google";
allPalindromicSubstrings(str);
return 0;
}
Also in C++:
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- c++ string manipulation
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- initialize vector of pointers c++
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- c++ pointers
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- unsorted array to bst
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- set mimetype validation in mongoose
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- double pointers C++
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- c++ remove space from string
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- how to make a switch case statement in c++
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- c++ remove element from vector
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- equal elements in two arrays in c++
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- c++ files
- Category
- C++