You are on page 1of 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.2
Student Name: Aaditya Narain Singh UID: 21BCS3653
Branch: BE-CSE Section/Group: CC-649(A)
Semester: VI Date of Performance: 17/01/24
Subject Name: AP Lab Subject Code: 21CSP-351

1. AIM: To implement the concept of String Matching.

2. OBJECTIVE:
The experiment aims to equip skills to write reliable, compact, and efficient
code quickly, emphasizing logic-building capabilities to tackle complex
problems using different approaches and work with linear data structures.

3. APPROACH/CODE:

a) Rotate String
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) Find the index of the first occurrence in a String


Given two strings needle and haystack, return the
index of the first occurrence of needle in haystack, or -1 if needle is not part
of haystack.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
a) CODE:

var rotateString = function(s, goal) {


let lenS = s.length;
while(lenS > 0){
s = s.substr(1, ) + s[0];
if(s == goal){
return true;
}
lenS--;
}
return false;

};

OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
b) CODE:
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
let index = haystack.indexOf(needle);
return index;
};

OUTPUT:
DEPARTMENT OF

4.TIME COMPLEXITY:
(A) O(N)
(B) O(N)

5.LEARNING OUTCOMES:
 Deal with Strings more proficiently.
 Being able to solve simple String problems presented using
different approaches .
 Make an optimised code

You might also like