2020-12-21

Isomorphic Strings

下面是 Isomorphic Strings 使用 string first 解的結果:

#!/usr/bin/env tclsh
#
# You are given two strings $A and $B. 
# Write a script to check if the given strings are Isomorphic. 
# Print 1 if they are otherwise 0.
#

proc isIsomorphic {str1 str2} {
    set len [string length $str1]
    if {$len != [string length $str2]} {
        return 0
    }

    for {set i 0} {$i < $len} {incr i} {
        set floc1 [string first [string index $str1 $i] $str1]
        set floc2 [string first [string index $str2 $i] $str2]

        if {$floc1 != $floc2} {
            return 0
        }
    }

    return 1
}

if {$argc == 2} {
    set string1 [lindex $argv 0]
    set string2 [lindex $argv 1]
} else {
    exit    
}

puts [isIsomorphic $string1 $string2]

沒有留言: