Java 如何最有效地检查唯一字符?
有人能帮我吗?我如何在现有代码中做一些小改动来检查唯一字符。如蒙回复,不胜感激Java 如何最有效地检查唯一字符?,java,arrays,string,Java,Arrays,String,有人能帮我吗?我如何在现有代码中做一些小改动来检查唯一字符。如蒙回复,不胜感激 public class UniqueStringCheck { public boolean checkUniqueString(String inputString){ String parseString = inputString.replaceAll("[^A-Za-z0-9]",""); int StringLength = parseString.length()
public class UniqueStringCheck {
public boolean checkUniqueString(String inputString){
String parseString = inputString.replaceAll("[^A-Za-z0-9]","");
int StringLength = parseString.length();
char[] sequenceOfString= parseString.toCharArray();
if(StringLength>0){
if(sequenceOfString.equals("[^A-Za-z0-9]")){
System.out.println("No unique char!");
return false;
}else{
System.out.println("Contains Unique char");
return true;
}
}
return true;
}
}
如果我正确解释问题(由唯一字符组成的字符串),有很多方法可以处理这个问题。这里有四种可能的不同效率的解决方案。根据需要进行修改
import java.util.HashSet;
import java.util.Set;
public class IsUnique {
/*With use of additional data structures
Time complexity can be argued to be O(1) because the for-loop will never
run longer than the size of the char-set (128, 256, or whatever UTF-8 is).
Otherwise, time complexity is O(n), where n is the size of the input string.
Space Complexity is O(1).
*/
public boolean isUnique(String s) {
//ExASCII = 256. Standard ASCII = 128. UTF-8 = ???
if (s.length() > 256) {
return false;
}
Set<Character> letterSet = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!letterSet.contains(c)) {
letterSet.add(c);
} else {
return false;
}
}
return true;
}
/*Without the use of additional data structures
Uses int as a bit mask.
Assumption: letters a-z, lowercase only
Time complexity, again, can be argued to be O(1) for the same reasons.
If that argument is not accepted, O(n), where n is the size of the input
string.
Space Complexity: O(1), but it uses a smaller amount of space than the
previous solution.
*/
public boolean isUniqueWithoutSet(String s) {
int check = 0;
if (s.length() > 26) {
return false;
}
for (int i = 0; i < s.length(); i++) {
int val = s.charAt(i) - 'a';
val = 1 << val;
if ((check & val) > 0) {
return false;
}
check |= val;
}
return true;
}
/*Without the use of additonal data structures.
Sorts the underlying character array of the string, iterates through it and
compares adjacent characters.
Time complexity: O(nlogn). Arguably, O(n^2) if the java quick-sort hits its
worst-case time (highly unlikely).
Space complexity: Arguably, O(1), because the largest the array will ever be
is the size of the character set. Otherwise, O(n), where n is the size of
the input string.
*/
public boolean badSolution(String s) {
if (s.length() > 26) {
return false;
}
char[] charArray = s.toCharArray();
java.util.Arrays.sort(charArray);
for (int i = 0, j = i + 1; i < charArray.length - 1; i++, j++) {
if (charArray[i] == charArray[j]) {
return false;
}
}
return true;
}
/*This solution is terri-bad, but it works. Does not require additional data
structures.
Time complexity: O(n^2)
Space complexity: O(1)/O(n).
*/
public boolean worseSolution(String s) {
if (s.length() > 256) {
return false;
}
for (int i = 0; i < s.length() - 1; i++) {
for (int j = i + 1; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
return false;
}
}
}
return true;
}
}
import java.util.HashSet;
导入java.util.Set;
公共类是唯一的{
/*使用额外的数据结构
时间复杂度可以说是O(1),因为for循环永远不会
运行时间超过字符集的大小(128、256或UTF-8的任意值)。
否则,时间复杂度为O(n),其中n是输入字符串的大小。
空间复杂度为O(1)。
*/
公共布尔值是唯一的(字符串s){
//ExASCII=256。标准ASCII=128。UTF-8=???
如果(s.长度()>256){
返回false;
}
Set letterSet=新HashSet();
对于(int i=0;i26){
返回false;
}
对于(int i=0;i26){
返回false;
}
char[]charArray=s.toCharArray();
java.util.Arrays.sort(charArray);
对于(int i=0,j=i+1;i256){
返回false;
}
对于(int i=0;i
让我们来分析一下这些方法
第一种方法使用集
检查字符串中的字符是否都是唯一的
第二种方法是我个人最喜欢的。它使用位屏蔽来确保唯一性。我认为与第一种方法相比,运行时效率提高了约8倍,但它们的复杂性仍然相同,对于大多数应用程序来说,差异可以忽略不计
第三种方法对字符进行排序,并将每个字符与其相邻字符进行比较
最后一种方法是对数组进行嵌套循环搜索。很糟糕,但它有效 请澄清:1)您所说的“唯一”字符串是什么意思?2) 您发布的代码有什么问题(除了格式不好)?格式已更正。这段代码甚至可以编译吗?好的,也许我速度很慢,但我仍然不知道您在这里试图做什么。@HovercraftFullOfEels让我在网站上更慢。。。下面我们还有一个完整的解决方案,我仍然不知道第一个方法中这个唯一的字符串是关于什么的,因为空间复杂度和时间复杂度是一样的。非常感谢。我很感激@Wadda_Wadda