From 6522012065712fb0ece31bff9ff10b38a83b10e1 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Thu, 22 Sep 2022 10:14:04 -0500 Subject: Initial commit --- LongestSubstringWithoutRepeat.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LongestSubstringWithoutRepeat.java (limited to 'LongestSubstringWithoutRepeat.java') 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 occur = new ArrayList(); + 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)); + } +} -- cgit v1.2.3