LintCode_chapter1_section1_two-s

2015-11-05  本文已影响57人  穆弋

容易
两个字符串是变位词
写出一个函数anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的
样例
输入 s="abcd" t="dcab"
输出 true

解题思路

判断题目给出的两个字符串是否是anagrams,即两个字符串的字母只是次序打乱.

some text
[some text](javascript:alert('xss'))
<button>asdasdasd</button>

参考答案

class Solution:
    """
    @param s: The first string
    @param b: The second string
    @return true or false
    """

    def anagram(self, s, t):
        # write your code here
        countFirst = self.countChars(s)
        countSecond = self.countChars(t)
        return countFirst == countSecond

    #
    def countChars(self, stringToCount):
        result = {}
        for item in stringToCount:
            try:
                result[item] += 1
            except KeyError:
                result[item] = 1
        return result
上一篇 下一篇

猜你喜欢

热点阅读