-5

I was looking at a solution for this problem:

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

The following solution was posted, but I am having trouble understanding what dict does. I've tried looking for documentation in C++. However, I have not found anything. Can someone explain how it works, and where I can find documentation?

int lengthOfLongestSubstring(string s) {
    vector<int> dict(256, -1);
    int maxLen = 0, start = -1;
    for (int i = 0; i != s.length(); i++) {
        if (dict[s[i]] > start)
            start = dict[s[i]];
        dict[s[i]] = i;
        maxLen = max(maxLen, i - start);
    }
    return maxLen;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 6
    It's the name of a variable – UnholySheep Nov 16 '22 at 20:39
  • 3
    It creates a vector with 256 items initialized to -1 – drescherjm Nov 16 '22 at 20:40
  • 4
    Here's the proper way to learn C++ fundamental concepts like this one: [open a C++ textbook](https://stackoverflow.com/questions/388242/) to Chapter 1, keep reading and doing its practice problems as you work your way through the book, each subject introduced and explained in an organized, step by step fashion. Here's the wrong way to learn C++: pick a random coding puzzle from a web site full of useless coding puzzles, then run keyword searches, trying to figure out what how each coding puzzle's solution program works. – Sam Varshavchik Nov 16 '22 at 20:40
  • 2
    https://en.cppreference.com/w/cpp/container/vector – user4581301 Nov 16 '22 at 20:41

1 Answers1

0

dict is just the name that was used for this vector<int>, first parameter is the the size of vector, second is value that should be assigned to all of its positions.

This is one of the possible ways to use its constructor, check the example on this page.

Diego Queiroz
  • 394
  • 8
  • 11
  • 1
    Thank you that makes sense now. I understand everything that is going I just assumed that was a constructor and not a variable. Thank you – Emmanuel Gil Nov 17 '22 at 17:11
  • I could have worded it better, but it was late and I was sleepy, but I'm glad it helped you understand how it works. – Diego Queiroz Nov 17 '22 at 17:58