Medium

A ciphertext alphabet is obtained from the plaintext alphabet by means of rearranging some characters. For example "bacdef...xyz" will be a simple ciphertext alphabet where a and b are rearranged.

A substitution cipher is a method of encoding where each letter of the plaintext alphabet is replaced with the corresponding (i.e. having the same index) letter of some ciphertext alphabet.

Given two strings, check whether it is possible to obtain them from each other using some (possibly, different) substitution ciphers.

Example

  • For string1 = "aacb" and string2 = "aabc", the output should be
    isSubstitutionCipher(string1, string2) = true.

    Any ciphertext alphabet that starts with acb... would make this transformation possible.

  • For string1 = "aa" and string2 = "bc", the output should be
    isSubstitutionCipher(string1, string2) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string string1

    A string consisting of lowercase English characters.

    Guaranteed constraints:
    1 ≤ string1.length ≤ 10.

  • [input] string string2

    A string consisting of lowercase English characters of the same length as string1.

    Guaranteed constraints:
    string2.length = string1.length.

  • [output] boolean

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isSubstitutionCipher(char *string1,char *string2)
{
	char cipher1[27]={'\0', }, cipher2[27]={'\0', };

	for(int i=0;i<strlen(string1);i++)
	{
		if(cipher1[string1[i]-'a']=='\0')
			cipher1[string1[i]-'a']=string2[i];
		else
			if(cipher1[string1[i]-'a']!=string2[i])
				return false;
		if(cipher2[string2[i]-'a']=='\0')
			cipher2[string2[i]-'a']=string1[i];
		else
			if(cipher2[string2[i]-'a']!=string1[i])
				return false;
	}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30
<Codesignal> Create Anagram  (0) 2020.04.19
<Codesignal> Strings Construction  (0) 2020.04.19
<Codesignal> HTML End Tag By Start Tag  (0) 2020.04.19
<Codesignal> Find Email Domain  (0) 2020.04.19

+ Recent posts