Two lines by Java 8 Stream API


  • 0
    X
    public int firstUniqChar(String s) {
        final int[] occurs = s.chars().collect(() -> new int[26], (a, ch) -> a[ch - 'a']++, (a, b) -> {});
        return IntStream.range(0, s.length()).filter(i -> occurs[s.charAt(i)-'a'] == 1).findFirst().orElse(-1);
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.