diff options
author | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
---|---|---|
committer | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
commit | 6522012065712fb0ece31bff9ff10b38a83b10e1 (patch) | |
tree | 743f3c13d08be9f60bad57b74e7a1fe7cc675e89 /LongestSubstringWithoutRepeat.java |
Initial commit
Diffstat (limited to 'LongestSubstringWithoutRepeat.java')
-rw-r--r-- | LongestSubstringWithoutRepeat.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/LongestSubstringWithoutRepeat.java b/LongestSubstringWithoutRepeat.java new file mode 100644 index 0000000..d01ecf3 --- /dev/null +++ b/LongestSubstringWithoutRepeat.java @@ -0,0 +1,27 @@ +import java.lang.*; +import java.util.*; + +class LongestSubstringWithoutRepeat { + public static int lengthOfLongestSubstring(String s) { + char[] arr = s.toCharArray(); + int max = 0; + ArrayList<Character> occur = new ArrayList<Character>(); + for (char c : arr) { + if (!occur.contains(c)) { + occur.add(c); + } else { + occur.clear(); + occur.add(c); + } + if (max < occur.size()) { + max = occur.size(); + } + } + return max; + } + + public static void main(String[] args) { + String s = "pwwkew"; + System.out.println(lengthOfLongestSubstring(s)); + } +} |