1 개요[ | ]
- Check the duplicate in string
- 문자열에서 중복 문자가 있는지 확인
2 예시 1[ | ]
C
CPU
0.0s
MEM
18M
0.0s
Copy
#include <stdio.h>
#include <string.h>
int allUnique(char *str) {
int i, j;
char *p = str;
int l = strlen(str);
for(i = 0; i < l - 1; i++) {
for(j = i + 1; j < l; j++) {
if(p[i] == p[j]) return 0;
}
}
return 1;
}
int main() {
printf("%d\n", allUnique("abcd")); // 1
printf("%d\n", allUnique("abcc")); // 0
}
1 0
3 예시 2[ | ]
C
Copy
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool allUnique(const char *str) {
if (str == NULL) {
return false;
}
bool isCharPresent[256] = {0};
int length = strlen(str);
for (int i = 0; i < length; i++) {
unsigned char ch = str[i];
if (isCharPresent[ch]) {
return false;
}
isCharPresent[ch] = true;
}
return true;
}
int main() {
printf("%d\n", allUnique("abcd")); // 1
printf("%d\n", allUnique("abcc")); // 0
}
Loading
4 같이 보기[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.
- 분류 댓글:
- C (7)
C, C++ 주석 ― YkhwongC, C++ 주석 ― John JeongC, C++ 주석 ― JmnoteC, C++ 주석 ― John JeongC언어 연결리스트 구현 ― 돌멩이C언어 연결리스트 구현 ― John JeongC언어 연결리스트 구현 ― 돌멩이