检查给定链接列表中是否存在字符串作为子序列
给定一个大小为N的字符串S和一个链表,任务是检查链表是否包含一个字符串作为子序列。如果它包含子序列,则打印Yes ,否则打印No 。
例子:
Input: S = “bad”, Linked List: b -> r -> a -> d -> NULL
Output: Yes
Input: S = “bad”, Linked List: a -> p -> p -> l -> e -> NULL
Output: No
方法:这个问题可以使用两个指针来解决,一个在字符串上,一个在链表上。现在,请按照以下步骤解决此问题:
- 创建变量i ,并将其初始化为 0。此外,创建指向链表头部的指针cur 。
- 现在,运行一个 while 循环,直到 i 小于N并且cur不是NULL并且在每次迭代中:
- 检查 S[i] 是否等于节点cur中的数据。如果将i递增到(i+1)并将cur移动到下一个节点。
- 如果不是,则仅将cur移动到下一个节点。
- 如果循环结束,则检查i是否变为N。如果是,则打印Yes ,否则打印No。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Node of Linked List
class Node {
public:
char data;
Node* next;
Node(char d)
{
data = d;
next = NULL;
}
};
// Function to check if the linked list contains
// a string as a subsequence
bool checkSub(Node* head, string S)
{
Node* cur = head;
int i = 0, N = S.size();
while (i < N and cur) {
if (S[i] == cur->data) {
i += 1;
}
cur = cur->next;
}
if (i == N) {
return 1;
}
return 0;
}
// Driver Code
int main()
{
Node* head = new Node('b');
head->next = new Node('r');
head->next->next = new Node('a');
head->next->next->next = new Node('d');
string S = "bad";
if (checkSub(head, S)) {
cout << "Yes";
}
else {
cout << "No";
}
}
Java
// Java code for the above approach
import java.util.*;
class GFG
{
// Node of Linked List
static class Node {
char data;Node next;
Node(char d)
{
data = d;
next = null;
}
};
// Function to check if the linked list contains
// a String as a subsequence
static boolean checkSub(Node head, String S)
{
Node cur = head;
int i = 0, N = S.length();
while (i < N && cur!=null) {
if (S.charAt(i) == cur.data) {
i += 1;
}
cur = cur.next;
}
if (i == N) {
return true;
}
return false;
}
// Driver Code
public static void main(String[] args)
{
Node head = new Node('b');
head.next = new Node('r');
head.next.next = new Node('a');
head.next.next.next = new Node('d');
String S = "bad";
if (checkSub(head, S)) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
}
// This code is contributed by gauravrajput1
Python3
# Python code for the above approach
# Node of Linked List
class Node:
def __init__(self, data):
self.data = data;
self.next = None;
# Function to check if the linked list contains
# a String as a subsequence
def checkSub(head, S):
cur = head;
i = 0;
N = len(S);
while (i < N and cur != None):
if (S[i] == cur.data):
i += 1;
cur = cur.next;
if (i == N):
return True;
return False;
# Driver Code
if __name__ == '__main__':
head = Node('b');
head.next = Node('r');
head.next.next = Node('a');
head.next.next.next = Node('d');
S = "bad";
if (checkSub(head, S)):
print("Yes");
else:
print("No");
# This code is contributed by Rajput-Ji
C#
// C# code for the above approach
using System;
public class GFG
{
// Node of Linked List
class Node {
public char data;
public Node next;
public Node(char d)
{
data = d;
next = null;
}
};
// Function to check if the linked list contains
// a String as a subsequence
static bool checkSub(Node head, String S)
{
Node cur = head;
int i = 0, N = S.Length;
while (i < N && cur!=null) {
if (S[i] == cur.data) {
i += 1;
}
cur = cur.next;
}
if (i == N) {
return true;
}
return false;
}
// Driver Code
public static void Main(String[] args)
{
Node head = new Node('b');
head.next = new Node('r');
head.next.next = new Node('a');
head.next.next.next = new Node('d');
String S = "bad";
if (checkSub(head, S)) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
Yes
时间复杂度: O(N)
辅助空间: O(1)