LoginSignup
0
1

More than 3 years have passed since last update.

「世界で闘うプログラミング力を鍛える本」Pythonコード解答例 - 1.4 回文の順列

Last updated at Posted at 2020-02-01

「世界で闘うプログラミング力を鍛える本」Pythonコード解答例 - 1.4 回文の順列

目次

CHAP1. 配列と文字列

  1. 重複のない文字列
  2. 同じ文字の数を数える
  3. URLify
  4. 回文の順列
  5. 一発変換
  6. 文字列圧縮
  7. 行列の回転
  8. "0"の行列
  9. 文字列の回転

Pythonコード解答例

def isPermutationOfPalindrome(phrase):

    phrase = phrase.lower()
    table = buildCharFrequencyTable(phrase)
    return checkMaxOneOdd(table)

def checkMaxOneOdd(table):

    foundOdd = False

    for count in range(len(table)):
        if table[count]%2 == 1:
            if foundOdd:
                return False
            foundOdd = True

    return True 

def getCharNumber(c):

    a = ord("a")
    z = ord("z")
    val = ord(c)

    if a <= val and val <= z:
        return val - a

    return -1

def buildCharFrequencyTable(phrase):

    table = [0] * 26

    for id in range(len(phrase)):
        c = phrase[id]
        x = getCharNumber(c)

        if x != -1 :
            table[x] = table[x] + 1

    return table

print(isPermutationOfPalindrome("Tact Coa"))
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1