【Java】【Python】【递归】判断两个字符串是否相同

2021-09-16 14:39:51 浏览数 (1)

代码语言:javascript复制
public class h {
    public static boolean isSameString1(String s1,String s2){
        return s1.equals(s2);
    }
    public static boolean isSameString2(String s1,String s2){
        if(s1.length()!=s2.length()) return false;
        if(s1.length()==0) return true;

        if(s1.charAt(0) != s2.charAt(0)) return false;
        return isSameString2(s1.substring(1),s2.substring(1));

    }
    public static void main(String[] args){
        System.out.println(isSameString1("ReganYue","ReganUye"));
        System.out.println(isSameString1("ReganYue","ReganYue"));
        System.out.println(isSameString2("ReganYue","ReganUye"));
        System.out.println(isSameString2("ReganYue","ReganYue"));
    }
}
代码语言:javascript复制
def isSameString(s1,s2):
    if(len(s1)!=len(s2)):
        return False;
    if(len(s1)==0):
        return True;
    if(s1[0]!=s2[0]):
        return False
    return isSameString(s1[1:],s2[1:])
def isSameString1(s1,s2):
    return (s1 == s2)

print(isSameString("Regan","rEGAN"))
print(isSameString("Regan","Regan"))
print(isSameString1("Regan","rEGAN"))
print(isSameString1("Regan","Regan"))

0 人点赞