{"id":4353,"date":"2022-04-22T05:28:37","date_gmt":"2022-04-22T05:28:37","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4353"},"modified":"2022-04-22T05:28:38","modified_gmt":"2022-04-22T05:28:38","slug":"longest-substring-without-repeating","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/04\/22\/longest-substring-without-repeating\/","title":{"rendered":"Longest Substring Without Repeating"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>The objective is to write a function which can identify the longest substring in a string which does not have repeating characters. Examples given below:<\/p>\n\n\n\n<p>String  &#8220;abcabcbb&#8221; . The answer would be 3 because the substring is abc<\/p>\n\n\n\n<p>String &#8220;bbbbb&#8221;. The answer would be 1 because the substring is b<\/p>\n\n\n\n<p>String &#8220;pwwkew&#8221;. The answer would be 3 because the substring is wke<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        int longestStart = -1;\n        int longestLength = -1;\n        int max = s.length();\n        if (max &lt; 1)\n            return 0;\n        if (max &lt; 2)\n            return 1;\n  \n        StringBuilder sb = new StringBuilder(&quot;&quot;);\n        int length = 0;\n        int counter = 0;\n        int startedAt = counter;\n        while (counter &lt; max) {\n                String check = s.substring(counter, counter+1);\n            \n                if (sb.toString().indexOf(check) == -1) {\n                    sb.append(check);\n                    length++;\n                    if (length &gt; longestLength) {\n                        longestLength = length;\n                        longestStart = counter;\n                    }\n                    counter++;\n                } else {\n                    sb = new StringBuilder(&quot;&quot;);\n                    length = 0;\n                    startedAt ++;\n                    counter = startedAt;\n                    \n                }\n        }\n    \n        \n        return longestLength;\n    }\n\n    public static void main(String&#x5B;] args) {\n        System.out.println(lengthOfLongestSubstring(&quot;pwwkew&quot;));\n    }\n}\n<\/pre><\/div>\n\n\n<p>The output is given below:<\/p>\n\n\n\n<p>3<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW The objective is to write a function which can identify the longest substring in a string which does not have repeating characters. Examples given <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/04\/22\/longest-substring-without-repeating\/\" title=\"Longest Substring Without Repeating\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":4308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366,319],"tags":[],"class_list":["post-4353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","category-java"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4353"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4353\/revisions"}],"predecessor-version":[{"id":4355,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4353\/revisions\/4355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/4308"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}