📜  门| GATE 2017 MOCK II |问题18(1)

📅  最后修改于: 2023-12-03 15:28:36.488000             🧑  作者: Mango

GATE 2017 MOCK II - Problem 18

Introduction

GATE 2017 Mock II - Problem 18 is a question related to programming and data structures. The problem requires the implementation of a function to perform a particular task. The question is a combination of concepts like dynamic programming and recursion.

In this question, we are given a string of lowercase characters, and we have to find the length of the longest substring without repeating characters. The question provides a function prototype, and we have to implement the function to solve the problem.

Problem Statement

Given a string s, find the length of the longest substring without repeating characters.

Function prototype: int longestSubstring(string s)

Approach

The most straightforward approach to solving this problem would be to generate all possible substrings of the given string and check if each substring has repeating characters or not. If it has repeating characters, we can discard the substring and move to the next one. We can maintain a variable to keep track of the maximum length encountered so far. Although this approach is correct, it is quite inefficient, as the number of substrings can be as high as O(n^2).

Thus, we need to come up with a more optimized approach to solving the problem. In this question, we can use a hash table to store the last position of each character encountered in the string. We will also maintain two pointers, start and end, to mark the start and end positions of the current substring.

We will iterate through the string, and at each position, we will check the hash table to see if the current character has already been encountered or not. If it has not been encountered yet, we can add it to the hash table and move to the next character. If it has been encountered previously, we will update the start pointer to the position of the last occurrence of the current character + 1. We will also update the hash table with the new position of the current character.

At each step, we will update the length of the longest substring encountered so far. The length of the current substring will be end - start + 1. We will return the maximum length encountered at the end of the iteration.

Code
int longestSubstring(string s) {
    int n = s.length();
    int i, j, start = 0, max_len = 0;
    unordered_map<char, int> hash_map;

    for(i = 0, j = 0; j < n; j++) {
        if(hash_map.find(s[j]) != hash_map.end()) {
            start = max(start, hash_map[s[j]] + 1);
        }
        hash_map[s[j]] = j;
        max_len = max(max_len, j - start + 1);
    }
    return max_len;
}
Complexity Analysis

The time complexity of this solution is O(n), where n is the length of the input string, as we are iterating through the string only once, and each operation inside the loop is a constant time operation.

The space complexity of this solution is O(k), where k is the number of unique characters in the input string, as we are storing the last position of each character in the hash table. Thus, the worst-case space complexity can be O(n), when all characters in the string are unique.