Skip to content

29455: 同构字符串

http://cs101.openjudge.cn/practice/29455/

给定两个字符串 st ,判断它们是否是同构的。

如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。

每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。

输入

第一行输入字符串s 第二行输入字符串t

输出

如果同构则输出"YES",否则输出"NO"

样例输入

paper
title

样例输出

YES
python
def is_isomorphic(s, t):
    if len(s) != len(t):
        return "NO"
    
    # 创建两个映射表
    s_to_t = {}
    t_to_s = {}
    
    for i in range(len(s)):
        char_s = s[i]
        char_t = t[i]
        
        # 检查 s 到 t 的映射
        if char_s in s_to_t:
            if s_to_t[char_s] != char_t:
                return "NO"
        else:
            s_to_t[char_s] = char_t
        
        # 检查 t 到 s 的映射
        if char_t in t_to_s:
            if t_to_s[char_t] != char_s:
                return "NO"
        else:
            t_to_s[char_t] = char_s
    
    return "YES"

# 输入
s = input().strip()
t = input().strip()

# 输出结果
print(is_isomorphic(s, t))