Java | 2 approaches | using string functions | Hashset
\nPLEASE UPVOTE IF IT HELPS YOU :)\n\n\nApproach 1: Using basic string functions\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n       
```\nPLEASE UPVOTE IF IT HELPS YOU :)\n\n\nApproach 1: Using basic string functions\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        if(s.length()==0)\n            return 0;\n        char ch=s.charAt(0);\n        String ans="";\n        ans=ans+ch;\n        int max=1;\n        for(int i=1;i<s.length();i++){\n            ch=s.charAt(i);\n            int index=ans.indexOf(ch);\n            if(index == -1){\n                ans=ans+ch;\n                max=Math.max(max,ans.length());\n            }\n            else{\n                ans=ans.substring(index+1)+ch;       //if s="dvdf", and when we encounter 2nd d, we need the new ans as "vd" and not just "d"\n            }\n        }\n        return max;\n    }\n}\n//TC: O(n)\n\n\nApproach 2: Acquire and Release(Sliding WIndow)\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        int acquire=0;\n        int release=0;\n        int max=0;\n        HashSet<Character> hash=new HashSet();\n        while(acquire<s.length()){\n            if(!hash.contains(s.charAt(acquire))){\n                hash.add(s.charAt(acquire));\n                acquire++;\n                max=Math.max(hash.size(),max);\n            }\n            else{\n                hash.remove(s.charAt(release));\n                release++;\n            }\n        }\n        return max;\n    }\n}\n```
['Sliding Window', 'Java']