0% found this document useful (0 votes)
34 views5 pages

Computer Science & Engineering: Department of

The document discusses three programming problems: 1) determining if a pattern matches a string, 2) finding the missing number in a sequence, and 3) finding the longest substring without repeating characters. It provides the code, time and space complexity analysis for each problem.

Uploaded by

NALIN KUMARI
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)
34 views5 pages

Computer Science & Engineering: Department of

The document discusses three programming problems: 1) determining if a pattern matches a string, 2) finding the missing number in a sequence, and 3) finding the longest substring without repeating characters. It provides the code, time and space complexity analysis for each problem.

Uploaded by

NALIN KUMARI
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/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.4

Student Name: Jai Badachiya UID: 21BCS5775


Branch: CSE Section/Group: 21BCSS_624-A
Semester: 6 Date: February 19, 2024
Subject Name: Advanced Programming
Subject Code: 21CSP-351

Aim:
• To Solve the Word Pattern
• To Solve the Missing number
• Longest substring without repeating characters

Problem 1:

Code and Output:


class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char, string> charToWord;
unordered_map<string, char> wordToChar;
istringstream iss(s);
string word;
int i = 0;
while (iss >> word) {
if (i == pattern.size()) {

JAI BADACHIYA 21BCS5775


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return false;
}
if (charToWord.count(pattern[i]) && charToWord[pattern[i]] != word) {
return false;
}
if (wordToChar.count(word) && wordToChar[word] != pattern[i]) {
return false;
}
charToWord[pattern[i]] = word;
wordToChar[word] = pattern[i];
++i;
}
return i == pattern.size();
}
};

Time complexity: O(n)


Space complexity: O(n)

JAI BADACHIYA 21BCS5775


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 2:

Code and output:


class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int expectedSum = n;
for (int i = 0; i < n; i++) {
expectedSum += i;
}
for (int num : nums) {
expectedSum -= num;
if (num <= 0 || num > n) {
continue;
}
int index = num - 1;
if (nums[index] != num) {
swap(nums[index], nums[num - 1]);
}
}
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return n;
}
};

JAI BADACHIYA 21BCS5775


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Time complexity: O(n)


Space complexity: O(1)

Problem 3:

Code and Output:


class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> ss;
int i=0;
int sol=0;
for (int j=0;j<s.size();j++) {
while(ss.count(s[j]))ss.erase(s[i++]);
ss.insert(s[j]);
sol = max(sol, j - i + 1);
}

JAI BADACHIYA 21BCS5775


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return sol;
}
};

Time complexity: O(n)


Space complexity: O(k)

Learning Outcomes:
 We learn about the concept of Hashing.
 We learn about the implementation of hashing.

JAI BADACHIYA 21BCS5775

You might also like