title: LeetCode (Longest Substring Without Repeating Characters)
body: <p>I was solving a problem on <strong>LeetCode</strong> called <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/" rel="nofollow noreferrer"><strong>&quot;Longest Substring Without Repeating Characters&quot;</strong></a>
  and I know that
  <br>- insertion and searching in <code>Hash Map</code> is O(1), for n times is <code>O(n)</code>
  <br>- insertion and searching in <code>Map</code> is O(log n), for n times <code>O(nlog(n))</code></p>
  <p>but when I was using <code>hash map</code> (unorderd_map) in cpp, the run time was <code>140ms</code>
  <br> and when using <code>map</code> in cpp, the run time was <code>40ms</code></p>
  <p>code below using hash map runs in <strong>140ms</strong>:</p>
  <pre class="lang-cpp prettyprint-override"><code>class Solution {
  public:
      int lengthOfLongestSubstring(string s) {
          
          int start=0, mx=0;
          unordered_map&lt;char, int&gt;um;
          
          for(int i=0; i&lt;s.size(); i++)
          {
              if(um.count(s[i]) &amp;&amp; start&lt;=um[s[i]])//exists
                  start=um[s[i]]+1;
              else 
                  mx=max(mx, i-start+1);
                  
              um[s[i]]=i;
              cout&lt;&lt;mx&lt;&lt;&quot;\n&quot;;
          }
          return mx;
      }
  };
  </code></pre>
  <p>and the same code with replacing hash map &quot;unordered_map&quot; with map runs in <strong>40ms</strong><br><br>
  <strong>so Why did this happen and not the opposite?</strong></p>