Professional Documents
Culture Documents
Worksheet - 2
Worksheet - 2
Worksheet - 2
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.
Algorithm leftRotate(s, d)
reverse(s, 0, d-l);
// Reverse
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
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