LeetCode-Notes
  • Introduction
  • Records of Practice
  • 关于Github 不支持密码问题
  • 面试题
    • 搜索广告
    • 多模态大模型
    • 刷题记录
  • 算法代码实现
  • Python
    • Python 笔记
  • Spark
    • PySpark
    • Spark Issues
    • Spark调优笔记
  • FeatureEngineering
    • Feature Cleaning
    • Feature Selection
    • Feature Transformation
    • Feature Crossing
  • Recommendation Algorithm
    • Recall-and-PreRank
      • Non-Negative Matrix Fatorization(NMF)
      • Fatorization Machine(FM)
      • User-base/Item-base实现
      • 多路召回实现
    • Ranking
      • NeuralFM
      • DeepFM
      • Deep&Cross network (DCN)
    • DeepLearning-Basic
      • Attention
      • Dropout
      • Batch Norm
  • Machine Learning
    • XGBoost
    • Cross Entropy Loss
    • Other models
  • Graph Neural Network
    • GNN-1-Basic
  • Big Data
    • Reservoir Sampling
  • SQL
    • SQL and PySpark functions
    • Query Film Infomation
    • Create, Insert and Alter Actor Table
    • Manage Employment Data
    • Manage Employment Data -2
  • DataStructure
    • Searching
      • Find power
      • 2 Sum All Pair II
      • Two Sum
      • Search in Rotate Array
      • Search In Shifted Sorted Array II
      • Search in 2D array
      • Three Sum with duplicated values
      • Median of Two Sorted Arrays
    • Array
      • Longest Consecutive Subarray
      • Merge Two Array in-place
      • Trapping water
      • Rotate matrix
    • Sorting
      • Merge intervals
      • 排序
      • 最小的k个数
      • Find TopK largest- QuickSelect快速选择 method
      • MergeSort Linkedlist
      • 第K大元素
    • LinkedList
      • Reverse LinkedList I
      • Reverse K-group linked list
      • Detect Start of Cycle
      • HasCycle
      • DetectCycle II
      • 链表的共同节点
      • 链表中倒数第k个节点
      • 删除链表倒数第k个节点
      • 合并两个链表
      • 在排序数组中查找元素的第一个和最后一个位置
      • 删除链表里面重复的元素-1
    • Tree
      • Find Tree height (general iteration method)
      • Check BST and Check CompleteTree
      • ZigZag Order traversal
      • Binary Tree diameter I
      • Maximum Path Sum Binary Tree
      • Maximum Path Sum Binary Tree II
      • Binary Tree Path Sum To Target III
      • Tree diameter 树的直径II
      • Tree ReConstruction
      • Check if B is Subtree of A
      • The Kth smallest in Binary Search Tree
      • 打印Tree的右视图
      • 二叉搜索树的后序遍历序列
      • 重建二叉树
      • 判断二叉树是否对称
      • Path Sum to Target in Binary Tree
      • Tree-PreOrder-InOrder-PostOrder
    • Heap&Queue
      • Top-K smallest
      • 滑动窗口最大值
      • Find the K-Largest
    • 合并k个已排序的链表
    • String
      • Reverse String
      • 最长不含重复字符的子字符串
      • 最长回文串
      • 最长回文子序列-DP
    • DFS/BFS
      • Number of island
      • Number of Provinces
      • All Permutations of Subsets without duplication
      • All Permutations of Subsets with duplication
      • Combinations Of Coins
      • All Subset I (without fixing size of subset, without order, without duplication)
      • All Subset of K size without duplication II
      • All Subset of K size III (with duplication without considering order)
      • All Permutation II (with duplication and consider order)
      • Factor Combination-质数分解
    • DynamicProgramming
      • DP-解题过程
      • Find Continuous Sequence Sum to Target
      • 1800. Maximum Ascending Subarray Sum
      • NC91 最长上升子序列
      • 查找string的编码方式个数
      • Maximum Product
      • Longest Common Substring
      • Longest Common Substring-II
      • minEditCost
      • Backpack I
      • Array Hopper I
      • Minimum distance between strings
      • 最大正方形
  • Big Data Algorithms
    • Big Data Processing Algorithms
      • Reservior Sampling
      • Shuffle
      • MapReduce
      • Bloom Filter
      • BitMap
      • Heap For Big Data
Powered by GitBook
On this page
  • 1. Link
  • 题目描述
  • 输入
  • 返回值
  • 说明
  • 输入
  • 返回值
  • 说明

Was this helpful?

  1. DataStructure
  2. DynamicProgramming

查找string的编码方式个数

DP; Medium; 字节

PreviousNC91 最长上升子序列NextMaximum Product

Last updated 4 years ago

Was this helpful?

1. Link

题目描述

有一种将字母编码成数字的方式:'a'->1, 'b->2', ... , 'z->26'。现在给一串数字,返回有多少种可能的译码结果示例1

输入

复制

"12"

返回值

复制

2

说明

2种可能的译码结果(”ab” 或”l”)

示例2

输入

复制

"31717126241541717"

返回值

复制

192

思想: DP

  1. 确定state : dp[i] = 在字符串0到i位置这个子字符里面的decoding ways个数

  2. 通过base case 找到transition 方程

    1. base case: when str = "n", n=0, -> dp[0] = 0 没有decoding way,因为不在1-26范围里面。 when str ="n", n>0, then dp[0] = 1

    2. 下一个状态 str = "nm", 它的decoding的方式有

      1. str = "n/m", 把新进来的char不管怎么样都总是当成一个digit,那么如果”m” >0 那么dp[1] += dp[0], 就是加上前面“n” 里面的decoding的方式个数

      2. str = "/nm" 把前面一个digit加上现在新进来的digit看成一个整体, 如果“nm”里面的n !=0 并且 int(nm) <26 那么它就是valid的字符,这样我们就在只考虑 "nm"之前的digit的编码个数。 “nm”之前没有东西,所以这里就默认成1。 所以 如果 i-2 ==-1, dp[i] += 1 否则 i-2>-1, dp[i] += dp[i-2]

      3. 由于编码的数字最多2个digit,所以到这里就不用再切分了。这样就根据限制条件找到转换方程了

  3. Time: O(n) Space: O(n)

#
# 解码
# @param nums string字符串 数字串
# @return int整型
#
class Solution:
    def solve(self , nums ):
        # write code here
        #
        # state dp[i] = number of decoding ways
        # base case "n", if n ==0, return 0, if "n" >0 and <9 return 1
        # "nm": if int(nm) =0 return 0 and  if 0<int(nm) < 11, return 1 "m"
        #  if int(nm)<=26: return 2  "n/m" and "nm"
        #
        # "nmk": consider one way separate k with previous char
        #: "nm/k" then this is equal to dp[i-1] decoding ways
        # consider "n/mk", combine previous char with k and think mk as one char
        # then we always let n, mk separate, then our solution is 
        # dp[i-2] = solve("n")
        #
        # because we can only combine two digiit at max, so no need to think
        # the previous cases
        #
        # so if "n/mk", int("mk")<26, then dp[i] = dp[i-1] + dp[i-2]
        # if int("mk") >26 or int("mk")==0, dp[i] = dp[i-1]
        # if "nm/k"  int("k") ==0, then this way is invalid, otherwise,
        #    dp[i] += dp[i-1]
    
            # if "n/mk", int("mk") ==0 or int("mk") >26, then this way is invalid
        #     otherwise: dp[i] += dp[i-2]
        # if two cases exist, then dp[i] = 0
        #
        # test case: "31" -> "3/1", "/31"
        #
        #
        #
        
        if nums == "0":
            return 0
        if len(nums) ==1:
            return 1
        dp =[0]*len(nums)
        
        for i in range( len(nums)):
            if i ==0:
                if nums[i] != "0":
                    dp[i] = 1
            else:
                # consider the case that we separate the last digit 
                # in this way "..../k"
                # if "k" >0 and <=9 then we add previous solution dp[i-1]
                if i-1 >=-1 and int(nums[i])>0:
                    dp[i] += dp[i-1]
                # consider the case we separate  the digit in this way
                #  ".../mk"
                # if "m" ==0, then "mk" is invalid so no need to add this case
                # if "m" != 0, then "mk" is valid so add the solution
                # "../mk" from dp[i-2]
                if i-2 >=-1 and int(nums[i-1]) !=0 and int(nums[i-1:i+1]) >0 and int(nums[i-1:i+1]) <=26:
                    if i-2 ==-1:
                        dp[i] += 1
                    else:
                        dp[i] += dp[i-2]
        return dp[-1]
        
        

说明

192种可能的译码结果

关联企业

把数字翻译成字符串_牛客题霸_牛客网
Logo