애너그램 체크이다, 두 개의 문자가 주어지고, 한 문자와 다른 문자가 동일한 알파벳으로 구성된 상태를 의미한다.

제한사항 중 주어지는 문자는 오로지 알파벳 소문자 이기 때문에  알파벳 배열을 만들어서 해결하자.

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }

        int[] count = new int[26];

        for (int i = 0; i < s.length(); i++) {
            count[s.charAt(i) - 'a']++;
        }
        
        for (int i = 0; i < t.length(); i++) {
            count[t.charAt(i) - 'a']--;
            
            if (count[t.charAt(i) - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}

알파벳 배열을 선정해주고 한 문자열에서 ++ 다른 문자열에서 -- 를 진행해준다. 만약 -- 과정 중 현재 얻어온 값이 0 보다 작다면? 고민할 것 없이 바로 false 날려주면 된다.

 

다른 좋은 풀이도 있어 소개해볼까 한다.

lass Solution {
    public boolean isAnagram(String s, String t) {
      if(s.length() != t.length())return false;
      char[] s1 = s.toCharArray();
      char[] t1 = t.toCharArray();
      Arrays.sort(s1);
      Arrays.sort(t1);
      for(int i=0;i<s1.length;i++){
        if(s1[i] != t1[i])return false;
      }
      return true;
  }
}

문자 들을 char 배열로 바꾼후 소팅한 후 비교하면 된다 쉽고 직관적인 풀이가 아닐 수 없다.

'PS > LeetcCode' 카테고리의 다른 글

33. Search in Rotated Sorted Array  (0) 2022.07.04
34. Find First and Last Position of Element in Sorted Array  (0) 2022.07.03
53. Maximum Subarray  (0) 2022.06.29
383. Ransom Note  (0) 2022.06.26
387. First Unique Character in a String  (0) 2022.06.26

문제 가 그 답지 어렵지 않다, 주어지는 두 개의 문자 중 하나의 문자를 이용해 다른 문자를 만들 수 있는지 물어보는 부분이다. 이것 역시 알파벳 소문자만 주어지기 때문에 배열로 바로 코드 치러가자.

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] alphabets = new int[26];
      for(char c : magazine.toCharArray()){
        alphabets[c-'a']++;
      }
      for(char c: ransomNote.toCharArray()){
        alphabets[c-'a']--;
      }
      for(int x:alphabets){
        if(x<0)return false;
      }
      return true;
    }
}

만들 문자들을 하나씩 쪼개 배열에 넣어 카운트를 해주고, 문자를 만들 때 하나씩 뺀 후 만약 0보다 작다면? 바로 리턴해주면 된다.

'PS > LeetcCode' 카테고리의 다른 글

33. Search in Rotated Sorted Array  (0) 2022.07.04
34. Find First and Last Position of Element in Sorted Array  (0) 2022.07.03
53. Maximum Subarray  (0) 2022.06.29
242. Valid Anagram  (0) 2022.06.26
387. First Unique Character in a String  (0) 2022.06.26

직관적인 문제다, 최초로 반복되지 않는 수 를 찾는 것이다. 소문자만 주어진다고 했으니 배열을 이용해서 작성해보자.

class Solution {
    public int firstUniqChar(String s) {
        int[] alphabets = new int[26];
    for(char c : s.toCharArray()){
      alphabets[c-'a']++;
    }
    for (int i = 0; i < s.length(); i++) {
      if(alphabets[s.charAt(i) - 'a'] == 1){
        return i;
      }
    }
    return -1;
            
    }
}

알파벳 배열을 만들고 숫자를 기록해 다음연산에서 활용해주는 것이다, 만약 대 소 문자에 특수문자까지 사용된다면, 해쉬 맵을 이용하면 더 좋은 선택지가 될 수 있다. 배열을 늘려도 상관은 없지만 비효율적이다.

'PS > LeetcCode' 카테고리의 다른 글

33. Search in Rotated Sorted Array  (0) 2022.07.04
34. Find First and Last Position of Element in Sorted Array  (0) 2022.07.03
53. Maximum Subarray  (0) 2022.06.29
242. Valid Anagram  (0) 2022.06.26
383. Ransom Note  (0) 2022.06.26

+ Recent posts