less than 1 minute read

Tags: ,

Repeated String

  • Imgur

  • python

    • 先說我這解法會出包 XDD
    • memory 爆炸!
# Complete the repeatedString function below.
def repeatedString(s, n):
    # repeate the s 到最小長度 len(s) 剛剛 超過 n 就好
    repeate_s = s * (int(n/len(s) + 1))
    # trim 到剛好的長度
    repeate_s = repeate_s[:n]
    result = repeate_s.count('a')
    return result
    
  • Imgur Imgur
  • 多看、多學、多聽、多謙卑

    • 如果沒記錯的話,之前掃 Angular 2019 年會活動照片
      有看到一個很像 workshop brainstorm 的 海報 裡面有一句話我超級認同&共鳴!

      • 多看別人的 coce

    • 非本科系出生的兒~ 就是要多學、多看、多聽、多謙卑阿~~~

    • 好扯多了~~ XDD

  • 知識分享好棒棒

全新學習後的成果

  • pyhton
# Complete the repeatedString function below.
def repeatedString(s, n):
    intact_s = n // len(s)
    remain_s = n % len(s)
    count_a = s.count('a')
    if remain_s != 0:
        return intact_s * count_a + (s[:remain_s]).count('a')
    else:
        return intact_s * count_a
// Complete the repeatedString function below.
function repeatedString(s, n) {
    let intact_s = Math.floor(n/(s.length))
    let remain_s = n % (s.length)
    let count_a = s.match(/a/g).length
    if (remain_s !== 0) {
        let left_s = s.substring(0, remain_s)
        let left_a_count = (left_s.match(/a/g) || []).length
        return (intact_s * count_a + left_a_count)
    } else {
        return intact_s * count_a
    }
}