检查字符串是否有唯一字符

Check if string has unique characters

本文关键字:唯一 字符 是否 字符串 检查      更新时间:2023-10-16

我知道如何检查字符串是否有唯一字符,但我想显示NOT unique,即使它们是不同的情况

eg - My algorithm

string = "dhAra" => UNIQUE

我认为更好的是它显示NOT UNIQUE,因为它有'a'两次

#include<iostream>
int main()
{
string str = "dhAra";
bool arr[128] = {0};
for (unsigned int i = 0; i < str.length() ; i++)
{
 int val = str[i];  //i think something needs to change here 
 cout << val << endl; 
 if(arr[val])
 { 
  cout << " not unique" ; 
  return 0; 
 }
 else
 { arr[val] = 1;} 
}
cout << "unique" ; 
return 0 ; 
}

您可以对所有字符使用touppertolower,以确保捕获仅在其情况下不同的重复字符:

int val = toupper(str[i]); // tolower would be fine as well

作为旁注,在c++中将true赋值给bool的规范方法是

arr[val] = true; // rather than arr[val] = 1

这是我想要做的正确方法之一,

// find if the string contains unique characters 
// uses extra space 
#include<iostream>
int main()
{
string str = "dhAr guptqwe fsklm";
bool arr[128] = {false};
for (unsigned int i = 0; i < str.length() ; i++)
{
 int val = toupper(str[i]);
 if(val == 32)
 continue; 
 if(arr[val])
 { 
  cout << " not unique" ; 
  return 0; 
 }
 else
 { arr[val] = true;} 
}
cout << "unique" ; 
return 0 ; 
}

谢谢

/**若要更改此模板,请选择"Tools | Templates"*并在编辑器中打开模板。*/包geeksforgeeks;

/* ***/进口java.util。,

公共类unique_char {

/**
 * @param args the command line arguments
 */
public static void quicksort(char array[], int p, int r)
{
    System.out.println("hello");
    if(r - p < 1)
          return;
    int pivot = p;
    int i = p + 1;
    int j = r;
    while(i < j)
    {
        while(array[i] > array[p] && i < r)
            i++;
        while(array[j] > array[p] && j > p)
            j--;
        if(i < j)
            swap(array,i,j);
    }
    swap(array,pivot,j);
    quicksort(array,p,j-1);
    quicksort(array,j+1,r);
}
public static void main(String[] args) 
{
    Scanner sc = new Scanner(System.in);
    char line[] = sc.nextLine().toCharArray();
    quicksort(line,0,line.length-1);
    //System.out.print(line);
    for(int i=0; i<line.length-1; i++)
    {
          if(line[i] == line[i+1])
               System.out.println("string dont have all char unique");
               break;
    }
}
private static void swap(char[] array, int pivot, int j)
{
    char t = array[pivot];
    array[pivot] = array[j];
    array[j] = t;
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

Dharag发布的最新答案基本有效,除了给出最终答案的一个小变化。

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int counter = 0;
string str = "dhAr guptqwe fsklm";
bool arr[128] = { false };
for (unsigned int i = 0; i < str.length(); i++)
{
    int val = toupper(str[i]);
    if (val == 32)
        continue;
    if (arr[val])
    {
        cout << "not uniquen";
        counter++;
        break;
        //return 0;
    }
    else
    {
        arr[val] = true;
    }
}
if (counter < 1) {
    cout << "uniquen";
}
return 0;
}

我使用了一个计数器变量,并在找到匹配的字符时自增。检查counter的值以检查整个字符串是否唯一。此外,一旦在字符串中找到匹配项,我们就可以退出该过程的其余部分,以提高效率。

我对这个问题的解决方案如下:

#include<iostream>
#include<string>
using namespace std;
//Method signature to determing if the string are unique
bool hasUniqueCharacters(string str);
int main(void){
    string str;
    getline(cin, str);
    if(hasUniqueCharacters(str)){
        cout<< " The Input string has all unique character" ;
    }
    else{
        cout<< " The input string does not have unique characters";
    }
    return 0;
}
/// <summary>
///     Implements the method hasUniqueCharacters to check if the input string has unique characters
/// </summary>
/// <Assumption>
///     This method assumes that all the characters in the input string are ASCII characters and the method uses a boolean array and makes the index of the ASCII equivalent field true. and traverses the whole string to veirfy if there are no duplicates.
/// </Assumption>
/// <param name="str">
///    This is the input string which needs to be checked.
/// </param>
/// <return datatype = boolean>
///     This method would return true if all the characters are unique in the input string.
/// </return datatype>
bool hasUniqueCharacters(string str){
    int strLength = str.size();
    bool characters[256];
    //Initializing all the index values in characters array to false.
    for(int i=0;i<256;i++){
        characters[i] = false;
    }
    //We are verifying if the character is already present in the array else making it true and perform this operation untill the string is complete.
    for(int i=0;i<strLength-1;i++){
        if(characters[int(str[i])])
            return false;
        else 
            characters[int(str[i])] = true;
    }
    return true;
}