forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSub string occurance using SLL.java
More file actions
86 lines (75 loc) · 1.49 KB
/
Sub string occurance using SLL.java
File metadata and controls
86 lines (75 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
class Node
{
Node next;
char data;
Node( char d)
{
next=null;
data=d;
}
}
class List
{
static Node add(char data)
{
Node newnode = new Node(data);
return newnode;
}
static Node string_to_SLL(String text,
Node head)
{
head = add(text.charAt(0));
Node curr = head;
for(int i=1;i<text.length();i++)
{
curr.next=add(text.charAt(i));
curr=curr.next;
}
return head;
}
public static void display(Node head)
{
Node curr;
curr=head;
while(curr!=null)
{System.out.print(" "+curr.data);curr=curr.next;}
if(head.next==null)
System.out.println("empty");
System.out.println();
}
public static void count_occurance(Node head1,Node head2)
{
Node curr1=head1;
Node curr2=head2;
int c=0;
while (curr1!=null)
{
if(curr1.data==curr2.data)
{ boolean t=true;
Node temp1=curr1;Node temp2=curr2;
while(temp2!=null)
{
if(temp2.data==temp1.data)
t=true;
else
t=false;
temp2=temp2.next;temp1=temp1.next;
}
if(t==true)
c++;
}
curr1=curr1.next;
}
System.out.println("occurance="+c);
}
public static void main(String[] args)
{
String s1 = "howarehoarlivahareit";
String s2 = "are";
Node head1 =null ; Node head2 =null;
head1 = string_to_SLL(s1, head1); head2 = string_to_SLL(s2, head2);
display(head1);display(head2);
count_occurance(head1,head2);
}
}