KEMBAR78
APFast | PDF
0% found this document useful (0 votes)
21 views2 pages

APFast

Uploaded by

ritikrk008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

APFast

Uploaded by

ritikrk008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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 java.util.Scanner;
public class Solution { public static boolean isMatch(String s, String p) {
if (p.isEmpty()) {
return s.isEmpty();
}
boolean isFirstMatch = (!s.isEmpty() && (s.charAt(0) == p.charAt(0) ||
p.charAt(0) == '.'));
if (p.length() >= 2 && p.charAt(1) == '*') {
return (isMatch(s, p.substring(2))) || (isFirstMatch &&
isMatch(s.substring(1), p));
}
else {
return isFirstMatch && isMatch(s.substring(1), p.substring(1));
}}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string S:");
String s = scanner.nextLine();
System.out.println("Enter the pattern P:");
String p = scanner.nextLine();
boolean result = isMatch(s, p);
System.out.println("Matching result: " + result);
scanner.close();
}
}

 Output:

You might also like