Isograms

Instructions

An isogram is a word that has no repeating letters, consecutive or non-consecutive.

Implement a function that determines whether a string that contains only letters is an isogram.

Assume the empty string is an isogram. Ignore letter case.

Example:

1
2
3
isIsogram "Dermatoglyphics" == true
isIsogram "moose" == false
isIsogram "aba" == false

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Set;
import java.util.stream.Collectors;

public class Isogram {

public static boolean isIsogram(String str) {
Set<Integer> uniqueChars = str.toLowerCase().chars()
.boxed()
.collect(Collectors.toSet());

return str.length() == uniqueChars.size();
}
}

Better Solution

1
2
3
4
5
public class IsogramSolution {
public static boolean isIsogram(String str) {
return str.length() == str.toLowerCase().chars().distinct().count();
}
}

Feeling

나름 잘했다고 생각했는데 distinct().count()를 몰라서 이런식으로 사용가능 할 줄 몰랐다…

Stream 관련해서 얼마나 아느냐도 알고리즘 문제푸는데 중요할 것 같다.

Share