牛客网编程题——字符串_确定两串乱序同构

题目描述

给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串中的空格。

给定一个string stringA和一个string stringB,请返回一个bool,代表两串是否重新排列后可相同。保证两串的长度都小于等于5000。

测试样例:

"This is nowcoder","is This nowcoder"
返回:true
"Here you are","Are you here"
返回:false

用到了python的Counter库,总体来说就是返回字典,每个字符串的个数

# -*- coding:utf-8 -*-
from collections import Counter
class Same:
    def checkSam(self, stringA, stringB):
        # write code here
        return len(stringA) == len(stringB) and Counter(stringA) == Counter(stringB)
发布了422 篇原创文章 · 获赞 256 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/qq_32146369/article/details/104933814