回答編集履歴
2
加筆
answer
CHANGED
@@ -25,10 +25,22 @@
|
|
25
25
|
return std::set<char>(str.begin(), str.end()).size() != str.size();
|
26
26
|
}
|
27
27
|
|
28
|
+
// 重複があったらtrueを返す
|
29
|
+
// そのさん
|
30
|
+
bool is_dup_3(const std::string& str) {
|
31
|
+
// コピーを作ってソートする
|
32
|
+
std::string copy = str;
|
33
|
+
std::sort(copy.begin(), copy.end());
|
34
|
+
// 隣接する同じ要素が見つかったらtrueを返す
|
35
|
+
return std::adjacent_find(copy.begin(),copy.end()) != copy.end();
|
36
|
+
}
|
37
|
+
|
28
38
|
int main() {
|
29
39
|
std::cout << std::boolalpha << is_dup_1("abcdefg") << std::endl;
|
30
40
|
std::cout << std::boolalpha << is_dup_1("abcdefd") << std::endl;
|
31
41
|
std::cout << std::boolalpha << is_dup_2("abcdefg") << std::endl;
|
32
42
|
std::cout << std::boolalpha << is_dup_2("abcdefd") << std::endl;
|
43
|
+
std::cout << std::boolalpha << is_dup_3("abcdefg") << std::endl;
|
44
|
+
std::cout << std::boolalpha << is_dup_3("abcdefd") << std::endl;
|
33
45
|
}
|
34
46
|
```
|
1
追記
answer
CHANGED
@@ -1,2 +1,34 @@
|
|
1
1
|
解決後でアレなのですが、
|
2
|
-
「対象文字列をソートし、隣接する文字が同じ箇所があるかを調べる」のもアリかと。
|
2
|
+
「対象文字列をソートし、隣接する文字が同じ箇所があるかを調べる」のもアリかと。
|
3
|
+
|
4
|
+
C++ならラクショーなんですけどね...
|
5
|
+
```C++
|
6
|
+
#include <iostream>
|
7
|
+
#include <string>
|
8
|
+
#include <algorithm>
|
9
|
+
#include <set>
|
10
|
+
|
11
|
+
// 重複があったらtrueを返す
|
12
|
+
// そのいち
|
13
|
+
bool is_dup_1(const std::string& str) {
|
14
|
+
// コピーを作ってソートする
|
15
|
+
std::string copy = str;
|
16
|
+
std::sort(copy.begin(), copy.end());
|
17
|
+
// 重複を取り除くことで文字列が短くなったらtrueを返す
|
18
|
+
return std::unique(copy.begin(),copy.end()) != copy.end();
|
19
|
+
}
|
20
|
+
|
21
|
+
// 重複があったらtrueを返す
|
22
|
+
// そのに
|
23
|
+
bool is_dup_2(const std::string& str) {
|
24
|
+
// 重複を許さない集合に詰め込んで、要素数が減ったら重複アリ
|
25
|
+
return std::set<char>(str.begin(), str.end()).size() != str.size();
|
26
|
+
}
|
27
|
+
|
28
|
+
int main() {
|
29
|
+
std::cout << std::boolalpha << is_dup_1("abcdefg") << std::endl;
|
30
|
+
std::cout << std::boolalpha << is_dup_1("abcdefd") << std::endl;
|
31
|
+
std::cout << std::boolalpha << is_dup_2("abcdefg") << std::endl;
|
32
|
+
std::cout << std::boolalpha << is_dup_2("abcdefd") << std::endl;
|
33
|
+
}
|
34
|
+
```
|