题目:
解法1:
boolean isogramTest(String word) {
word = word.replace("-","").replace(" ","");boolean flag = true;for (int i = 0; i <word.length() - 1; i++) {
for (int j = i + 1; j < word.length() - i ; j++) {
if (word.charAt(i)== word.charAt(j)) {
flag = false;}}}return flag;}
解法2:
boolean isIsogram(String phrase) {
phrase = phrase.replace("-", "").replace(" ", "").toLowerCase();return (phrase.chars().distinct().count() == phrase.length());}
解法3:
boolean isIsogram(String phrase) {
phrase = phrase.replaceAll("(?:\\s|-)","").toLowerCase();return phrase.chars().distinct().count() == phrase.length();}
总结:
解法一是博主自己的解法,在阅读完很多个别人的解法后,挑选出比较优秀的解法2和解法3,解法二和解法三的思路是通过字符串中不同的字符的个数来和字符串的长度比较,不需要自己再编写算法了,比较简便明了。