[go: up one dir, main page]

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

Experiment 1.2

Uploaded by

suhaninayak78
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)
15 views3 pages

Experiment 1.2

Uploaded by

suhaninayak78
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

Experiment 1.

2
Student Name: Suhani nayak UID: 21BET1061
Branch: BE-CSE Section/Group: CC-601B
Semester: 6 Date of Performance:23-01-2024
Subject Name: Advance Programming lab
Subject Code:21ITH-251

1. Aim: String Matching.


● Find the Index of the First Occurrence in a String
● Rotate String

2. Objective:
● 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 therightmost position.For
example, if s = "abcde", then it will be "bcdea"after one shift.
● Given two strings needleand haystack, return the index of the first occurrence of
needlein haystack, or -1if needleis not part of haystack.

3. Code and output:


class Solution {
public boolean rotateString(String s,
Stringgoal) {if (s.length() != goal.length())
{
return false;
}
if (s.equals(goal)
{
return true;
}
for (int i = 0; i < s.length(); i++) {
String rotated = s.substring(i) +
s.substring(0, i);
if (rotated.equals(goal))
{
return true;
}
}
return false;
}
}
//SUHANI NAYAK

Name: Suhani nayak UID: 21BET1061


2. Find the Index of the First Occurrence in a String
public class Solution {
public int repeatedStringMatch(String a, String b) {
int n = a.length();
int m = b.length();
StringBuilder repeatedA = new StringBuilder();
int repeats = 0;
while (repeatedA.length() < n + m) {
repeatedA.append(a);
repeats++;
if (repeatedA.toString().contains(b)) {
return repeats;
}
}
return -1;
}
public static void main(String[] args) {
Solution solution = new Solution();
String a1 = "abcd";
String b1 = "cdabcdab";
System.out.println(solution.repeatedStringMatch(a1, b1));
String a2 = "a";
String b2 = "aa";
System.out.println(solution.repeatedStringMatch(a2, b2));
}
}

Name: Suhani nayak UID: 21BET1061


4. Learning Outcomes:
• Knowledge of Fundamental String Matching Algorithms
• Techniques for Matching Patterns
• Describe and clarify fundamental string matching algorithms, including Boyer-Moore,
Rabin-Karp, Knuth-Morris-Pratt (KMP), and brute force.

Name: Suhani nayak UID: 21BET1061

You might also like