Leetcode 242. Effective ectopic letter word ---- python

1. Title Description

Given two strings s and t, t write a function to determine whether the ectopic letters of the word s.

Example 1 :
Input: s = "anagram", t = "nagaram"
Output: true

Example 2 :
Input: s = "rat", t = "car"
Output: false

Note:
You can assume that the string contains only lowercase letters.

2. Problem-solving ideas

Ectopic two letter strings, satisfy two characteristics:
(1) equal to the length
(2) comprises a number of letters appear consistent

I found out from an instance of summary characteristics, write the code, high efficiency Duohahaha.

3. code implementation

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if(len(s) != len(t)):
            return False
        else:
            setS = set(s)
            for i in setS:
                if(s.count(i) != t.count(i)):
                    return False
            return True

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/u013075024/article/details/93305366