计算用户给出的String中的唯一字符

我必须编写一个程序来计算用户给出的字符串中的唯一字符。 例如,“abc”返回3,“aabbccd”返回4.我不允许使用高级Java类,如Map,Set等。我只能使用数组,字符串,for循环,while循环,if语句。 我试图使用嵌套循环,但我对如何编写第二个for循环的算法感到困惑。

public static int countUniqueCharacters(String input){ String orgInput = input.toLowerCase(); int count = 0; int stringLength = input.length(); for( int i = 0; i ji-1; j--){ char temp = orgInput.charAt(i); if (temp == orgInput.charAt(j)){ count++; 

这非常容易:)

 public static int countUniqueCharacters(String input) { boolean[] isItThere = new boolean[Character.MAX_VALUE]; for (int i = 0; i < input.length(); i++) { isItThere[input.charAt(i)] = true; } int count = 0; for (int i = 0; i < isItThere.length; i++) { if (isItThere[i] == true){ count++; } } return count; } 

输入“aab”的示例

第一个for-cycle进行3次,每次为一个char。

“a”的值是97,所以它将isItThere [97]变为true,然后涉及第二个“a”,这也是相同的,isItThere [97]再次设置为true(因此不做任何改变)。

在涉及“b”之后,char“b”的值为98,因此isItThere [98]设置为true。

然后你有第二个for-cycle,你循环遍历所有isItThere数组。 如果您发现任何真实陈述,则增加计数。 在我们的例子中,你发现isItThere [97]和isItThere [98]为真正的陈述,它意味着你增加两次并返回2。

使用Java 8,您可以执行以下操作:

 public static long countUniqueCharacters(String input) { return input.chars() .distinct() .count(); } 

这将创建一个charIntStream ,然后仅获取区分值,然后计算出现的次数。

另一个解决方案:

 public static int countUniqueCharacters(String input) { String buffer = ""; for (int i = 0; i < input.length(); i++) { if (!buffer.contains(String.valueOf(input.charAt(i)))) { buffer += input.charAt(i); } } return buffer.length(); } 

每个字符的第一次出现都存储在buffer 。 因此, buffer包含所有字符,因此buffer.length()提供所需的计数。

如果您坚持使用Java 7,您可以使用ArrayList并只为其添加唯一值,然后返回ArrayList的大小,即使计数为零也应该始终有效。

  import java.util.ArrayList; public int getUniqeCount( String arg ) { ArrayList unique = new ArrayList(); for( int i = 0; i < arg.length(); i++) if( !unique.contains( arg.charAt( i ) ) ) unique.add( arg.charAt( i ) ); return unique.size(); } 
 public static int countUniqueChars (String buf) { HashSet hash = new HashSet<>(); buf = buf.toUpperCase(); for (int i = 0; i < buf.length(); i++) hash.add(buf.charAt(i)); return hash.size(); } 
 public static long calculateDistinctSubStringSum(String text) { Map map = new HashMap(); char[] charAarry = text.toCharArray(); for (int i = 0; i < charAarry.length; i++) { map.put(charAarry[i] + "", 1); } return map.size(); } 

这就是我的计算方法,但我仍在寻找任何快速方法。 如果有人知道请回答。

您可以使用HashSet集合来计算字符串中的唯一元素。 它只允许独特的元素。

代码片段

 public static int uniqueCount(String str) { HashSet al = new HashSet(); char[] arr= str.toCharArray(); for (int i=0; i 

请参阅此链接以获取完整代码: ideone

尝试查看以下代码是否可以帮助您:

 String myString = ""; for(int i=0; i< word.length(); i++) { if(myString.indexOf(word.charAt(i)) == -1) { System.out.println(word.charAt(i)); myString = myString + word.charAt(i); } } return myString.length(); 
 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter String:"); int length = scanner.nextInt(); char[] ch1 = new char[length]; String[] input = new String[length]; for (int i = 0; i < length; i++) { String userInput = scanner.next(); input[i] = userInput; ch1= userInput.toCharArray(); Arrays.sort(ch1); System.out.println(ch1); } 

字符串中的唯一字符:

这是一个基本的Java访谈主题,访问者想要检查HashSet或indexOf的知识(在Java 7的情况下)。 我们来回答一个问题。 让我们说面试告诉你
检查String是否唯一:HashSet确保唯一性,换句话说,HashSet中的每个对象只出现一次。 因此,我们将使用HashSet。

 import java.util.HashSet; import java.util.Set; public class Abc { public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); if( isUnique(aa) ) { System.out.println("All characters are unique"); }else { System.out.println("All characters are not unique"); } } public static boolean isUnique(String a ) { Set< Character> set = new HashSet<>(); char[] charArray =a.toCharArray(); for(Character ch :charArray) { if(!set.add(ch)) { return false; }//if }//foreach return true; } } 

GINI的输出将是:所有字符都不是唯一的

现在,计算独特的字符。 在这里我们也将使用HashSet,因为它的独特性。

 import java.util.HashSet; public class practice11 { public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); System.out.println(countUniqueCharacters(aa)); } public static int countUniqueCharacters(String a) { char[] charArray = a.toCharArray(); HashSet set = new HashSet(); for(int i = 0 ; i< charArray.length ; i++) { set.add(charArray[i]); }//for return set.size() ;//This will give 3 } } 

基尼的输出为3(将考虑杜松子酒)。

indexOf方法:indexOf()返回第一次出现的字符的索引,然后我们与-1进行比较。

 public class Abc { public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); String t = " "; for (int i = 0; i < aa.length(); i++) { int pp = aa.charAt(i) ; if(t.indexOf(aa.charAt(i)) == -1 ) { t = t + aa.charAt(i); }//if }//for System.out.println(t );// This will give => gin System.out.println(t.length()); // this will give 3 }//main }//end 

在Java 8中,这非常简单。 我们只需要使用chars()。distinct()。count()。 但返回类型会很长。

 class Abc{ public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); System.out.println( countUnique(aa)); } private static long countUnique(String aa) { // this will give 3(gin. another i will be not be counted as we have used distinct()) return aa.chars().distinct().count() ; } } 

另一个经典的采访问题:在字符串中查找第一个非重复字符或在字符串中查找第一个唯一字符。 使用HashMap的知识可以解决这个问题。

 class Abc{ public static void main(String[] args) { String a = "GinaRani" ; // Output will be G System.out.println( firstNonRepeatingCharacter(a) ); }//main public static Character firstNonRepeatingCharacter(String a){ Map map = new HashMap<>(); char[] charArray = a.toCharArray(); for( Character ch : charArray){ if( map.containsKey(ch) ) { map.put(ch, map.get(ch) +1 ) ; } else{ map.put(ch, 1); } }//for // 1st non repeating character for( int i = 0 ; i < a.length(); i ++ ){ char chh = a.charAt(i); if( map.get(chh) == 1 ){ System.out.println("first non repeating character in the String is : "); return chh ; }//if }//for return null; }//firstNonRepeatingCharacter }//end 

我们将在Python中使用list comprehension来完成这项工作。 我们没有使用set()运算符。 在Python中:

 string = 'GiniGinaProtijayi' unique = [] [ unique.append(ch) for ch in string if ch not in unique ] lengthofUniqueCharacters = len(unique) print("length of Unique Characters => " ,lengthofUniqueCharacters) print("as a list => ",unique) print("as a string => " , ''.join(unique)) 

如果您被允许使用Java集,则以下代码是可读的,紧凑的并且可以顺利地完成工作

 public static int countUniqueChar(String word){ Set wordSet = new HashSet<>(); for(Character c : word.toCharArray()) wordSet.add(c); return wordSet.size(); }