0% found this document useful (0 votes)
22 views3 pages

Ap 2

Uploaded by

Amit Kumar
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)
22 views3 pages

Ap 2

Uploaded by

Amit Kumar
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/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.2

Student Name: Amit Kumar UID: 21BCS7024


Branch: CSE Section/Group: 640-B
Semester: 6th Date of Performance:23-01-24
Subject Name:Advance Programming Lab-2 Subject Code: 21CSP-351

1. Aim:
To demonstrate the concept of String-Matching algorithms.

2. Objective:

A) Given two strings s and goal, return true if and only if s can become goal after some number
of shifts on s.A shift on s consists of moving the leftmost character of s to the rightmost position.
For example, if s = "abcde", then it will be "bcdea" after one shift.

B) Given two strings a and b, return the minimum number of times you should repeat string a
so that string b is a substring of it. If it is impossible for b to be a substring of a after repeating it,
return -1.

3. Script and Output:

A. Rotate String:
class Solution {
public:
bool rotateString(string s, string goal) {
return s.length() == goal.length() && (s + s).find(goal) != string::npos;
}
};

Amit Kumar 21BCS7024


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
OUTPUT:

B. Repeated String Match


class Solution {
public:
int repeatedStringMatch(string a, string b) {
const int n = ceil((double)b.length() / a.length());
string s;

for (int i = 0; i < n; ++i)


s += a;

if (s.find(b) != string::npos)
return n;
if ((s + a).find(b) != string::npos)
return n + 1;
return -1;
}
};

Amit Kumar 21BCS7024


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Amit Kumar 21BCS7024

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy