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

时间:2013-03-25 19:56:28

标签: c++ algorithm

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

例如 - 我的算法

string =“dhAra”=>独一无二的

我认为更好的是它显示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 ; 
}

5 个答案:

答案 0 :(得分:5)

您可以对所有字符使用touppertolower,以确保您捕获的重复项仅在其大小写中有所不同:

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

作为旁注,在C ++中将true分配给bool的规范方法是

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

虽然两种方式都可以。

答案 1 :(得分:0)

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

// 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 ; 
}

谢谢

答案 2 :(得分:0)

/ *  *要更改此模板,请选择“工具”|模板  *并在编辑器中打开模板。  * / 包geeksforgeeks;

/ **  *  *   / import java.util。;

public class 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.
}

}

答案 3 :(得分:0)

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 unique\n";
        counter++;
        break;
        //return 0;
    }
    else
    {
        arr[val] = true;
    }
}

if (counter < 1) {
    cout << "unique\n";
}
return 0;
}

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

答案 4 :(得分:0)

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

#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;
}