[코테 풀이] Longest Substring Without Repeating Characters

시내·2024년 6월 5일

Q_3) Longest Substring Without Repeating Characters

출처 : https://leetcode.com/problems/longest-substring-without-repeating-characters/

Given a string s, find the length of the longest substring without repeating characters.

class Solution {
    public int lengthOfLongestSubstring(String s) {
          String substring = "";
        int max = 0;
        int count = 0;
        ArrayList<Character> arrayList = new ArrayList<>();
        while (count < s.length()) {
            for (int i = count; i < s.length(); i++) {
                if (arrayList.contains(s.charAt(i))) {
                    if (substring.length() > max) {
                        max = substring.length();

                    }
                    arrayList.clear();
                    count++;
                    substring = "";
                    break;

                } else {
                    arrayList.add(s.charAt(i));
                    substring += s.charAt(i);
                }
            }
        }
        return max;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글