본문 바로가기

코테31

[Algorithm][Array] 문제 풀이 #15 - Group Anagrams 문제 링크 https://leetcode.com/problems/group-anagrams/ 문제 설명 배열에 있는 문자들끼리의 아나그램을 구하고 그룹핑 시켜라 내 코드 public List groupAnagrams(String[] strs) { List results = new ArrayList(); List stringList = new ArrayList(); stringList.add(strs[0]); results.add(stringList); for(int i = 1; i < strs.length; i++) { boolean add = true; for(List result : results) { String r = result.get(0); if(isAnagram(r, strs[i])) { r.. 2022. 7. 28.
[Algorithm][Array] 문제 풀이 #14 - Find All Anagrams in a String 문제 링크 https://leetcode.com/problems/find-all-anagrams-in-a-string/ 내 코드 public List findAnagrams(String s, String p) { List list = new ArrayList(); if (s.length() < p.length()) { return list; } int[] sChar = new int[26]; int[] pChar = new int[26]; for (int i = 0; i < p.length(); i++) { pChar[p.charAt(i) - 'a'] += 1; } int si = 0; for (int i = 0; i < s.length(); i++) { sChar[s.charAt(i) - 'a'] +=.. 2022. 7. 27.
[Algorithm][Array] 문제 풀이 #13 - Longest Repeating Character Replacement 문제 링크 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 내 코드 public int characterReplacement(String s, int k) { int[] arr = new int[128]; int left = 0; int right = 0; int max = 0; int result = 0; while(right k) { arr[s.charAt(left)]--; left++; } result = Math.max(result, right - .. 2022. 7. 25.
[Algorithm][Array] 문제 풀이 #12 - Longest Substring Without Repeating Characters 문제 링크 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 내 코드 public int lengthOfLongestSubstring(String s) { int result = 0; Integer[] alphabets = new Integer[128]; int left = 0; int right = 0; while (right < s.length()) { char c = s.charAt(right); Integer index = alphabets[c]; if(index != null && left 2022. 7. 24.