C 程序:检查两个给定的字符串是否彼此同构

原文:https://www.geeksforgeeks.org/c-program-to-check-if-two-given-strings-are-isomorphic-to-each-other/

给定两个字符串str1str2,任务是检查两个给定字符串是否彼此同构

如果str1的每个字符到str2的每个字符都有一个一对一的映射,则说两个字符串是同构。 并且str1中每个字符的所有出现都映射到str2中的相同字符。

示例

输入str1 = "egg", str2 ="add"

输出Yes

说明

str1中的 ASCII 值 101 的'e'映射为str2中的 ASCII 值 97 的'a'str1中 ASCII 值 103 的'g'映射为str2中的 ASCII 值 100 的'd'

输入str1 = "eggs", str2 = "addd"

输出No

哈希方法:有关基于Hashmap的方法,请参考先前的文章

时间复杂度O(N)

辅助空间O(256)

基于 ASCII 值的方法:该想法与上述方法类似。 请按照以下步骤解决问题:

  1. 初始化大小为 256 的两个数组。

  2. 遍历给定字符串的字符,并在i位置递增等于该字符的 ASCII 值的索引。

  3. 如果在字符映射中没有冲突,则打印Yes。 否则,打印No

下面是上述方法的实现:

C

// C Program to implement 
// the above approach 

#include <stdio.h> 
#include <string.h> 
#include <stdbool.h> 

// Function to check and return if strings 
// str1 and str2 are ismorphic 
bool areIsomorphic(char *str1, char *str2) 
{ 
    // If the length of the strings 
    // are not equal 
    if (strlen(str1) != strlen(str2)) { 
        return false; 
    } 

    // Initialise two arrays 
    int arr1[256] = { 0 }, arr2[256] = { 0 }; 

    // Travsersing both the strings 
    for (int i = 0; i < strlen(str1); i++) { 

        // If current characters don't map 
        if (arr1[(int)str1[i]]  
        != arr2[(int)str2[i]]) { 
            return false; 
        } 

        // Increment the count of characters 
        // at their respective ASCII indices 
        arr1[(int)str1[i]]++; 
        arr2[(int)str2[i]]++; 
    } 
    return true; 
} 

// Driver Code 
int main() 
{ 
    char s1[] = "aab", s2[] = "xxy"; 

    if (areIsomorphic(s1, s2)) 
        printf("Yes\n"); 
    else
        printf("No\n"); 

    return 0; 
} 

输出

Yes

*时间复杂度O(N)

辅助空间O(256)