Find all Palindrome Strings in given Array of Strings


We need to find all palindrome strings from the given array in this problem. The string is a palindrome if we can read it the same from the start and end.

We can use two approaches to check whether the string is a palindrome. In the first approach, we reverse the string and compare it with the original string, and in the second approach, we keep comparing the string characters from start to last.

Problem statement − We have given an array containing the N strings. We need to print all the strings of the array, which are palindrome. If the array doesn’t contain any palindrome strings, print −1.

Sample examples

Input

array[] = {"tut", "point", "tutorial", "nanan", "great"}

Output

tut, nanan

Explanation − The ‘tut’ and ‘nanan’ are palindrome strings from the given array.

Input

 array[] = {"point", "tutorial", "shubham", "great"}

Output

-1

Explanation − The array doesn’t contain any palindromic string, so it prints −1.

Input

 array[] = {}

Output

-1

Explanation − The array is empty, so it prints −1.

Approach 1

In this approach, we traverse the array and check whether a particular string is palindrome. We will use the reverse() method to check if the string is palindrome.

Algorithm

Step 1 − Define the ‘palindromes’ list to store all palindrome strings.

Step 2 − Traverse the array.

Step 3 − Execute the checkForPalindrome() function to check whether the string at the pth index is a palindrome.

Step 3.1 − Store the string into the ‘temp’ string in the function.

Step 3.2 − Reverse the alpha string using the reverse() method.

Step 3.3 − Return the boolean value after comparing the temp and alpha.

Step 4 − If the particular string is a palindrome, push it to the ‘palindromes’ list.

Step 5 − Return the palindromes list.

Example

#include <bits/stdc++.h>
using namespace std;

bool checkForPalindrome(string &alpha) {
    // Copy the string
    string temp = alpha;
    // Reverse the string
    reverse(alpha.begin(), alpha.end());
    // If the string and its reverse is equal, return true. Else return false.
    return temp == alpha;
}
vector<string> FindAllPalindrome(string array[], int N) {
    vector<string> palindromes;
    // traverse the array
    for (int p = 0; p < N; p++) {
        // If the string is a palindrome, push it into vector
        if (checkForPalindrome(array[p])) {
            palindromes.push_back(array[p]);
        }
    }
    return palindromes;
}
int main() {
    string array[] = {"tut", "point", "tutorial", "nanan", "great"};
    int N = sizeof(array) / sizeof(array[0]);
    // Print the required answer
    vector<string> list = FindAllPalindrome(array, N);
    if (list.size() == 0)
        cout << "-1";
    else {
        cout << "The palindrome strings from the given array are : ";
        for (string str : list) {
            cout << str << ", ";
        }
    }
    return 0;
}

Output

The palindrome strings from the given array are : tut, nanan,

Time complexity− O(N*K), where N is the array length, and K is the maximum length of the string.

Space complexity − O(N + K), as we store the original string to a ‘temp’ string and store palindrome strings in the list.

Approach 2

In this approach, we print the palindrome strings directly rather than storing them in the list. Also, we match the string's characters from start to end to check whether the string is palindrome rather than using the reverse() method.

Algorithm

Step 1 − Initialize the ‘palCnt’ variable with 0 to store the count of palindrome strings in the array.

Step 2 − Traverse the string and execute the checkForPalindrome() function by passing the string as a parameter to check if the string is a palindrome.

Step 2.1 − Initialize the left and right variables in the checkForPalindrome() function.

Step 2.2 − Keep traversing the string until the left is less than the right.

Step 2.3 − If the character at the left and right index is different, return false.

Step 2.4 − Increase left by 1, and decrease right by 1.

Step 2.5 − At last, return true.

Step 3 − If the string at the pth index is a palindrome, increase the value of ‘palCnt’ by 1, and print the string.

Step 4 − At last, if the value of the ‘palCnt’ is 0, print −1.

Example

#include <bits/stdc++.h>
using namespace std;

bool checkForPalindrome(string &alpha) {
    // Initialize the start and end index
    int left = 0;
    int right = alpha.length() - 1;
    // traverse the string
    while (left < right) {
        // compare characters from start and end
        if (alpha[left] != alpha[right]) {
            return false;
        }
        // change value of pointers
        left++;
        right--;
    }
    return true;
}
void FindAllPalindrome(string array[], int N) {
    int palCnt = 0;
    // traverse the array
    for (int p = 0; p < N; p++) {
        // If the string is palindrome, push it into vector
        if (checkForPalindrome(array[p])) {
            palCnt++;
            cout << array[p] << ", ";
        }
    }
    if(palCnt == 0){
        cout << " -1 ";
    }
}
int main(){
    string array[] = {"cbc", "bed", "pop", "mam", "navjivan"};
    int N = sizeof(array) / sizeof(array[0]);
    cout << "The palindrome strings from the given array are : ";
    FindAllPalindrome(array, N);       
    return 0;
}

Output

The palindrome strings from the given array are : cbc, pop, mam,

Time complexity− O(N*K) to check whether a string is a palindrome.

Space complexity − O(1) as we don’t use any dynamic space.

The second approach is more optimized as it uses fewer spaces than the first one. However, the time complexity of both approaches is the same. Programmers can also try to count non−palindromic strings in the given array for more practice.

Updated on: 14-Aug-2023

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements