Worksheet - 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DEPARTMENT OF COMPUTER

SCIENCE & ENGINEERING


Experiment - 2

Student Name:Vedprakash Tanwar UID: 20BCS9640


Branch: B.E-CSE Section/Group: 615/B
Semester:6th
Subject Name: Competitive Coding-II Subject Code:20CSP-351

1. Aim: ð To implement the concept of string manipulation.

2. Objective:
ð The objective is to build problem solving capability and to learn the basic concepts of data structures.
ð The implementation of rotate string which shows and brushes up the concept of strings and can be solved
through various approaches.
ð The implementation of repeated string matching in which the concept of npos was introduced.

3. Leetcode code and output:

ROTATE STRING CODE=

Algorithm leftRotate(s, d)

reverse(s, 0, d-l);

// Reverse

substring s[0..d-1] reverse(s, d, n-l);

// Reverse substring s[d..n-l] reverse(s, 0, 11-1);

// Reverse whole string.

Right rotate string s by d (Assuming d <= n) rightRotate(s,d)

We can also call above reverse steps with d = n-d.


leftRotate(s, n-d)
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING

CODE:

class Solution {
public boolean rotateString(String s, String goal) {
return (s.length()==goal.length() && (s+s).contains(goal));

}
}

OUTPUT=
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING

REPEATED STRING MATCHING CODE:

Algorithm:
Function to check if a number is a substring of other or not

Check for substring from starting from i ' th index of main string for (int i = e; i <= N int
j; check for pattern match for (j if (repl[i + j]

Function to find Minimum number of times A has to be repeated such that is a substring of it

CODE:

class Solution {
public int repeatedStringMatch(String A, String B) {
String strA = A;
int repeat = B.length()/ A.length();
int count=1;
for(int i=0; i<repeat+2; i++){
if(A.contains(B)){
return count;
}
else {
A += strA;
count++;
}
}
return -1;
}
}
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING

OUTPUT=

Learning Outcomes :-
1. Learnt about the concept of string
2. Learnt the concept of array rotation

You might also like