3

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

例如 - 我的算法

字符串 = "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 ; 
}
4

5 回答 5

5

您可以在所有字符上使用toupperortolower以确保捕获仅在大小写不同的重复项:

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

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

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

尽管两种方式都可以正常工作。

于 2013-03-25T19:58:35.577 回答
0

/* * 要更改此模板,请选择工具 | 模板 * 并在编辑器中打开模板。*/ 包 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.
}

}

于 2014-02-11T12:19:25.460 回答
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 ; 
}

谢谢

于 2013-03-25T20:18:09.663 回答
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;
}
于 2018-01-11T18:52:13.350 回答
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;
}

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

于 2016-03-15T03:13:21.180 回答