Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 1.11 KB

Question_387.md

File metadata and controls

49 lines (40 loc) · 1.11 KB

LeetCode Records - Question 387 First Unique Character in a String

Attempt 1: Use an int array to record

class Solution {
    public int firstUniqChar(String s) {
        int[] saved = new int[26];

        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            saved[arr[i] - 'a']++;
        }

        for (int i = 0; i < arr.length; i++) {
            if (saved[arr[i] - 'a'] == 1) {
                return i;
            }
        }

        return -1;
    }
}
  • Runtime: 4 ms (Beats: 95.41%)
  • Memory: 45.28 MB (Beats: 24.21%)

Attempt 2: Use indexOf() and lastIndexOf()

class Solution {
    public int firstUniqChar(String s) {
        int min = Integer.MAX_VALUE;

        for (char ch = 'a'; ch <= 'z'; ch++) {
            int startIndex = s.indexOf(ch);
            if (startIndex != -1 && startIndex == s.lastIndexOf(ch)) {
                min = Math.min(min, startIndex);
            }
        }

        return min == Integer.MAX_VALUE ? -1 : min;
    }
}
  • Runtime: 4 ms (Beats: 95.41%)
  • Memory: 45.10 MB (Beats: 36.64%)