列のパターンマッチング

856 ワード

タイトルの説明
2つの文字列A、Bについて.効率的なアルゴリズムを設計して、BがAで初めて現れる開始位置を見つけてください.BがAに表示されない場合は、-1を返します.
2つの文字列AとBと、それらの長さlenaとlenbを指定して、テーマの答えを返してください.
テストサンプル:
"acbc",4,"bc",2
  :2

方法1:
# -*- coding:utf-8 -*-

class StringPattern:
    def findAppearance(self, A, lena, B, lenb):
        if lena

方法2:(すばらしい!)
# -*- coding:utf-8 -*-

class StringPattern:
    def findAppearance(self, A, lena, B, lenb):
        return A.index(B) if B in A else -1

また、プログラム内のindexをfindに変更してもよいが、この2つの方法の機能は同じであり、わずかに異なるだけであり、indexとfindについては具体的に以下の2つのリンクを参照することができる.
https://www.runoob.com/python/att-string-index.html
https://www.runoob.com/python/att-string-find.html