Skip to content

Instantly share code, notes, and snippets.

@itayB
Created August 8, 2016 18:28
Show Gist options
  • Save itayB/c99bb298ab543e1340e8c59e0ff6c2cd to your computer and use it in GitHub Desktop.
Save itayB/c99bb298ab543e1340e8c59e0ff6c2cd to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <iostream>
using namespace std;
/* Get string with number at the beginning of it and returns the number value. for example: "120ab" --> 120 */
int getNumber(char* str) {
int i = 0;
int size = strlen(str);
int value = 0;
for (i=0 ; i < size ; i++) {
if (str[i] < '0' || str[i] > '9')
return value;
value *= 10;
value += (str[i]-'0');
}
return value;
}
/* gets string with numbers and replcae them with ? sign */
void textTransform(char* src, char* dst) {
int N = strlen(src);
int i=0;
int j=0;
while (i < N) {
if ((src[i] >= 'a' && src[i] <= 'z') || (src[i] >= 'A' && src[i] <= 'Z')) {
dst[j] = src[i];
i++;
j++;
}
else if (src[i] >= '0' && src[i] <= '9') {
int value = getNumber(&src[i]);
for (int t=0 ; t < value ; t++) {
dst[j] = '?';
j++;
}
while (src[i] >= '0' && src[i] <= '9') {
i++;
}
}
}
dst[j] = '\0';
}
int solution(char *S, char *T) {
int i = 0;
int N = strlen(S);
int M = strlen(T);
// TODO: make transformation of both strings to ? form.
char newS[100000] = "";
char newT[100000] = "";
textTransform(S,newS);
textTransform(T,newT);
// TODO: make sure that length of both strings are equal! (after numbers transformation)
int newN = strlen(newS);
int newM = strlen(newT);
if (newN != newM) {
return 0;
}
// TODO: iterate over both strings and compare (? is like a joker)
for (i=0 ; i < newN ; i++) {
if (newS[i] != '?' && newT[i] != '?' && newS[i] != newT[i])
return 0;
}
return 1;
}
int main() {
/* Test getNumber function. */
printf("%d\n", getNumber("1"));
printf("%d\n", getNumber("12"));
printf("%d\n", getNumber("123"));
printf("%d\n", getNumber("120"));
printf("%d\n", getNumber("120a"));
printf("%d\n", getNumber("120ab"));
/* Test textTransform function */
char dst[100000] = "";
textTransform("A2le", dst);
printf("%s\n",dst);
char* a1 = "A2le";
char* b1 = "2pl1";
if (solution(a1,b1) == 1) {
printf("Pass");
}
else {
printf("Failed");
}
printf(" for %s %s\n", a1,b1);
char* a2 = "a10";
char* b2 = "10a";
if (solution(a2,b2) == 1) {
printf("Pass");
}
else {
printf("Failed");
}
printf(" for %s %s\n", a2,b2);
char* a3 = "ba1";
char* b3 = "aAd";
if (solution(a3,b3) == 0) {
printf("Pass");
}
else {
printf("Failed");
}
printf(" for %s %s\n", a3,b3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment