Files
C-Plus-Plus/d4/db8/longest__substring__without__repeating__characters_8cpp.html
2024-10-10 14:30:39 +00:00

238 lines
16 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: others/longest_substring_without_repeating_characters.cpp File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(1); });
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('d4/db8/longest__substring__without__repeating__characters_8cpp.html','../../'); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Classes</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">longest_substring_without_repeating_characters.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a class="el" href="../../dd/d4f/class_solution.html">Solution</a> for Longest Substring Without Repeating Characters problem.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;unordered_map&gt;</code><br />
<code>#include &lt;deque&gt;</code><br />
<code>#include &lt;string&gt;</code><br />
<code>#include &lt;cassert&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for longest_substring_without_repeating_characters.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../db/de5/longest__substring__without__repeating__characters_8cpp__incl.svg" width="463" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Classes</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">class &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d21/class_longest___substring.html">Longest_Substring</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">Class that solves the Longest Substring Without Repeating Characters problem. <a href="../../da/d21/class_longest___substring.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a483bb8ccf42aaf7375a83e91490eda1e" id="r_a483bb8ccf42aaf7375a83e91490eda1e"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a> ()</td></tr>
<tr class="memdesc:a483bb8ccf42aaf7375a83e91490eda1e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <br /></td></tr>
<tr class="separator:a483bb8ccf42aaf7375a83e91490eda1e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p><a class="el" href="../../dd/d4f/class_solution.html">Solution</a> for Longest Substring Without Repeating Characters problem. </p>
<p>Problem link: <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/description/">https://leetcode.com/problems/longest-substring-without-repeating-characters/description/</a></p>
<p>Intuition: 1) The intuition is straightforward and simple. We track the frequency of characters. 2) Since we can't use a string to track the longest substring without repeating characters efficiently (as removing a character from the front of a string isn't O(1)), we optimize the solution using a deque approach.</p>
<p>Approach: 1) Initialize an unordered_map to track the frequency of characters. 2) Use a deque for pushing characters, and update the result deque (<code>res</code>) with the current deque (<code>temp</code>) whenever we find a longer substring. 3) Use a while loop to reduce the frequency from the front, incrementing <code>i</code>, and removing characters from the <code>temp</code> deque as we no longer need them. 4) Return <code>res.size()</code> as we are interested in the length of the longest substring.</p>
<p>Time Complexity: O(N) Space Complexity: O(N)</p>
<p>I hope this helps to understand. Thank you! </p><dl class="section author"><dt>Author</dt><dd><a href="../../github.com/ashish5kmax" target="_blank">Ashish Kumar Sahoo</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on successful execution. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 107</span> {</div>
<div class="line"><span class="lineno"> 108</span> <a class="code hl_function" href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 110</span>}</div>
<div class="ttc" id="alongest__substring__without__repeating__characters_8cpp_html_a483bb8ccf42aaf7375a83e91490eda1e"><div class="ttname"><a href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a></div><div class="ttdeci">static void tests()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition</b> longest_substring_without_repeating_characters.cpp:92</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d4/db8/longest__substring__without__repeating__characters_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="579" height="192"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="a483bb8ccf42aaf7375a83e91490eda1e" name="a483bb8ccf42aaf7375a83e91490eda1e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a483bb8ccf42aaf7375a83e91490eda1e">&#9670;&#160;</a></span>tests()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void tests </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementations. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 92</span> {</div>
<div class="line"><span class="lineno"> 93</span> <a class="code hl_class" href="../../da/d21/class_longest___substring.html">Longest_Substring</a> soln;</div>
<div class="line"><span class="lineno"> 94</span> assert(soln.<a class="code hl_function" href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">lengthOfLongestSubstring</a>(<span class="stringliteral">&quot;abcabcbb&quot;</span>) == 3);</div>
<div class="line"><span class="lineno"> 95</span> assert(soln.<a class="code hl_function" href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">lengthOfLongestSubstring</a>(<span class="stringliteral">&quot;bbbbb&quot;</span>) == 1);</div>
<div class="line"><span class="lineno"> 96</span> assert(soln.<a class="code hl_function" href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">lengthOfLongestSubstring</a>(<span class="stringliteral">&quot;pwwkew&quot;</span>) == 3);</div>
<div class="line"><span class="lineno"> 97</span> assert(soln.<a class="code hl_function" href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">lengthOfLongestSubstring</a>(<span class="stringliteral">&quot;&quot;</span>) == 0); <span class="comment">// Test case for empty string</span></div>
<div class="line"><span class="lineno"> 98</span> assert(soln.<a class="code hl_function" href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">lengthOfLongestSubstring</a>(<span class="stringliteral">&quot;abcdef&quot;</span>) == 6); <span class="comment">// Test case for all unique characters</span></div>
<div class="line"><span class="lineno"> 99</span> assert(soln.<a class="code hl_function" href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">lengthOfLongestSubstring</a>(<span class="stringliteral">&quot;a&quot;</span>) == 1); <span class="comment">// Single character</span></div>
<div class="line"><span class="lineno"> 100</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;All test cases passed!&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 101</span>}</div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="aclass_longest___substring_html"><div class="ttname"><a href="../../da/d21/class_longest___substring.html">Longest_Substring</a></div><div class="ttdoc">Class that solves the Longest Substring Without Repeating Characters problem.</div><div class="ttdef"><b>Definition</b> longest_substring_without_repeating_characters.cpp:37</div></div>
<div class="ttc" id="aclass_longest___substring_html_af8a3672bc8d95e7da4f9a45e5e2e387d"><div class="ttname"><a href="../../da/d21/class_longest___substring.html#af8a3672bc8d95e7da4f9a45e5e2e387d">Longest_Substring::lengthOfLongestSubstring</a></div><div class="ttdeci">int lengthOfLongestSubstring(std::string s)</div><div class="ttdoc">Function to find the length of the longest substring without repeating characters.</div><div class="ttdef"><b>Definition</b> longest_substring_without_repeating_characters.cpp:44</div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d4/db8/longest__substring__without__repeating__characters_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.svg" width="482" height="192"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_9510827d0b234b3cc54b29892f217477.html">others</a></li><li class="navelem"><a class="el" href="../../d4/db8/longest__substring__without__repeating__characters_8cpp.html">longest_substring_without_repeating_characters.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0 </li>
</ul>
</div>
</body>
</html>