Permutation problems

Hi, I'm new to c++ and I'm not very good with it, but I've been asked to write a function that takes a string as input and returns true if this string is a permutation of the alphabet and false other wise.
Now I was provided with a function for the generation for a random permutation but I don't if it works or how to get it to work.
What I was given was:

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";

for the alphabet and this for the generation of a random permutation

string create_permutation(unsigned seed)
{
srand(seed);
string permutation = ALPHABET;
// using built-in random generator:
random_shuffle(permutation.begin(), permutation.end());
return permutation;
}

I put all that in with the <iostream> header and the <algorithm> header and it didn't work .
Do I need other headers or do I need to change something in the function, or is it just that it won't work without the actual test as well?
I'm sorry for any rambling, this has just got me really stuck.
The character is strange. Perhaps it isn't an 8-bit char?

The std::random_shuffle() is deprecated. Use std::shuffle() instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// a shuffle algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::shuffle
#include <string>
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock

using std::string;
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,`' "; // different chars here

string create_permutation(unsigned seed)
{
  string permutation = ALPHABET;
  std::shuffle( permutation.begin(), permutation.end(), std::default_random_engine(seed) );
  return permutation;
}

int main () {
  // obtain a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

  string text = create_permutation( seed );

  std::cout << "shuffled alphabet:\n";
  std::cout << text << '\n';
  return 0;
}
Thank you that is great and has helped me with some other functions. However how would I make it so that an inputted string of characters, checked and would come out as true if it is a permutation of the given alphabet and false if not. I'm sorry if this is really basic stuff.
std::is_permutation() http://en.cppreference.com/w/cpp/algorithm/is_permutation

bool is_permutation = std::is_permutation( input.begin(), input.end(), alphabet.begin(), alphabet.end() );
closed account (D80DSL3A)
In case using a canned method spoils the exercise...
is std::string str; a permutation of ALPHABET ?
It's easy.
Checklist:
if( str.length() != 26 ) return false;// wrong # of letters

If every letter is in the range 'A' to 'Z' and appears only once then str is a permutation of ALPHABET.
1
2
3
4
5
6
for( char c : str )// for each char in str
{
    if( c < 'A' || c > 'Z' ) return false;// illegal character found
     if (c has been found already ) return false;   
}
 return true;// all tests passed 

I'll leave 'c has been found already' to you.
My method involves an array of 26 bools.

edit: code correction
Last edited on
Or sort both words. Strings have op==.
closed account (D80DSL3A)
Yes, but what's the best we can get from an STL sort routine? O(n)log(n) ?
It's a small point, especially because n is so small (26), but it's a point because my algorithm is O(n) all day long, tyvm!
The canned (presumably optimized) solution is at most O(n*n) and at least O(n) (except for length mismatch case that is O(1) ), so yours might not last all day after all?

1
2
3
if( str.length() != 26 ) return false;
// OR
if( str.length() != ALPHABET.length() ) return false;
> The canned (presumably optimized) solution is at most O(n*n)

The standard algorithm is optimised for the general case; not for a special case. The fun2code algorithm is optimised for the special case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is_permutation( const std::string& a, const std::string& b  )
{
    if( a.size() != b.size() ) return false ;

    static constexpr std::size_t N = std::numeric_limits<unsigned char>::max() + 1 ;

    std::array<std::size_t,N> aa {} ;
    for( unsigned char c : a ) ++aa[c] ;

    std::array<std::size_t,N> bb {} ;
    for( unsigned char c : b ) ++bb[c] ;

    return aa == bb ;
}
Topic archived. No new replies allowed.