DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Complex Problems
Student Name: Ritik Kumar UID: 21BCS1054
Branch: BE-CSE Section/Group: 21BCS-614-B
Semester: 6th Date of Performance: 13.02.24
Subject Name: Advanced Programming Lab-2 Subject Code: 21CSP-351
1. Aim: Given two strings S and P where S consists of only lowercase English
alphabets while P consists of lowercase English alphabets as well as special
characters ‘.’ and ‘*’, the task is to implement a function to test regular
expression such that: '.' Matches any single character. '*' Matches zero or more
of the preceding elements. Note: For each appearance of the character ‘*',
there will be a previous valid character to match. Examples: Input: s = "aaa",
p = "a" Output: false Explanation: "a" does not match the entire string "aaa".
Code:
import [Link];
public class Solution { public static boolean isMatch(String s, String p) {
if ([Link]()) {
return [Link]();
}
boolean isFirstMatch = (![Link]() && ([Link](0) == [Link](0) ||
[Link](0) == '.'));
if ([Link]() >= 2 && [Link](1) == '*') {
return (isMatch(s, [Link](2))) || (isFirstMatch &&
isMatch([Link](1), p));
}
else {
return isFirstMatch && isMatch([Link](1), [Link](1));
}}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the string S:");
String s = [Link]();
[Link]("Enter the pattern P:");
String p = [Link]();
boolean result = isMatch(s, p);
[Link]("Matching result: " + result);
[Link]();
}
}
Output: