-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ0102_isPermutation.cpp
49 lines (45 loc) · 1.17 KB
/
Q0102_isPermutation.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "Q0102_isPermutation.h"
#include <string>
#include <iostream>
using namespace CodingInterview;
void Q0102_isPermutation::Run()
{
std::string sub = "red";
std::string main = "def";
bool result = isPermutation(sub, main);
if (result)
std::cout << sub << " is permutation of " << main << ".\n";
else
std::cout << sub << " is not permutation of " << main << ".\n";
std::cout << "--------------------------------------\n";
char* sub2 = "dog";
char* main2 = "go ahead";
result = isPermutation(sub2, main2);
if (result)
std::cout << sub2 << " is permutation of " << main2 << ".\n";
else
std::cout << sub2 << " is not permutation of " << main2 << ".\n";
std::cout << "--------------------------------------\n";
}
bool Q0102_isPermutation::isPermutation(const char* subString, const char* mainString)
{
for(const char* it = subString; *it != NULL; ++it)
{
if (strchr(mainString, *it) == NULL)
{
return false;
}
}
return true;
}
bool Q0102_isPermutation::isPermutation(const std::string& subString, const std::string& mainString)
{
for (char c : subString)
{
if (mainString.find(subString) == std::string::npos)
{
return false;
}
}
return true;
}