Check if a String is Present in the given Linked List as a Subsequence


In this problem, we will check if the linked list contains the string as a subsequence. We can iterate the list and string together to check whether a string is present as a subsequence in the linked list.

Problem statement − We have given a string of size N. Also, we have given a linked list of the dynamic length containing the alphabetical characters. We need to check whether the linked list contains the string as a subsequence.

Sample examples

Input

'e' -> 'h' -> 'e' -> 'k' -> 'h' -> 'e' -> 'l' ->'o' -> 'l' -> 'o' -> 'a', str = ‘hello’

Output

Yes

Explanation − The linked list contains the string as a subsequence.

Input

a’ -> ‘b’ -> ‘d’ -> ‘m’ -> ‘n’ -> ‘p’ -> ‘o’ -> ‘l’, str = “hello”

Output

No

Explanation − The Linked list doesn’t contain a string as a substring.

Input

a’ -> ‘a’, str = ‘aaaaa’

Output

No

Explanation − The string length is 5, and the Linked list contains only 2 nodes. So, it is not possible that the linked list contains the string as a subsequence.

Approach 1

In this approach, we will create a linked list from an array of characters. After that, we will match the next character of the string and the current character of the node. If both matches, we move to the string's next character and node in the linked list. Otherwise, we move to the next node of the linked list only. In such a way, we can check if the string is present in the linked list.

Algorithm

Step 1 − Create the linked list from the array by inserting the nodes into the linked list.

Step 2 − Initialize the ‘current’ node with the start node, ‘p’ with 0, and ‘len’ with the string’s length.

Step 3 − Make iterations until p < len and the current node is not null.

Step 4 − If Str[p] == current−>ch is true, increase the value of ‘p’ by 1.

Step 5 − Move to the next node of the linked list.

Step 6 − If p is equal to the ‘len’, return true.

Step 7 − At the end of the function, return true.

Example

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

// creating the struct listNode
struct ListNode {
    int ch;
    ListNode *next;
};
// adding nodes to the linked list
void addNode(struct ListNode **start, int ch) {
    // creating a new node
    struct ListNode *temp = new struct ListNode();
    // add ch to the node
    temp->ch = ch;
    temp->next = NULL;
    // If the list is empty, add a node to the list
    if (*start == NULL) {
        *start = temp;
    } else {
        // If the list has some nodes, append the node at the end of the list
        struct ListNode *pointer1 = *start;
        while (pointer1->next != NULL)
        {
            pointer1 = pointer1->next;
        }
        pointer1->next = temp;
    }
}
bool isSubSeqPresent(ListNode *start, string Str) {
    ListNode *current = start;
    int p = 0, len = Str.size();
    //  Traverse the list and string simultaneously
    while (p < len && current) {
        // If a character in the list and at the pth index of the string is the same, increment p by 1.
        if (Str[p] == current->ch) {
            p += 1;
        }
        // Move to the next node
        current = current->next;
    }
    if (p == len) {
        return true;
    }
    return false;
}
int main() {
    int list[] = {'e', 'h', 'e', 'k', 'h', 'e', 'l', 'o', 'l', 'o', 'a'};
    string str = "hello";
    int len, p;
    // create an empty linked list
    struct ListNode *start = NULL;
    len = sizeof(list) / sizeof(list[0]);
    // inserting characters of the array to a linked list
    for (p = 0; p < len; p++)
        addNode(&start, list[p]);
   bool res = isSubSeqPresent(start,str);
   if(res){
       cout << "The given string is present as a subsequence in the list." << endl;
   } else {
       cout << "The given string is not present as a subsequence in the list." << endl;
   }
    return 0;
}

Output

The given string is present as a subsequence in the list.

Time complexity− O(N + K), where N is the length of the string, and K is the length of the linked list.

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

We learned to write C++ code to check whether a string is present as a subsequence in the linked list. Programmers can also write the code to check whether the given string is present as a subsequence in the linked list.

Updated on: 14-Aug-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements