IT技術(shù)小屋

          秋風(fēng)秋雨,皆入我心

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            38 隨筆 :: 1 文章 :: 19 評(píng)論 :: 0 Trackbacks
          Distinct Subsequences

          Given a string S and a string T, count the number of distinct sub sequences of T in S.
          A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
          Here is an example:
          S = "rabbbit", T = "rabbit"
          Return 3.

          這兩個(gè)題目很相似,狀態(tài)方程是f(i, j) = f(i - 1, j) + S[i] == T[j]? f(i - 1, j - 1) : 0 Where f(i, j) is the number of distinct sub-sequence for T[0:j] in S[0:i].
          數(shù)組初始情況是第一列全部是1,代表T字符串是空串,S和自己做匹配。實(shí)現(xiàn)代碼如下:
           1 public class DistinctSubsequences {
           2     public int numDistinct(String S, String T) {
           3         int[][] f = new int[S.length() + 1][T.length() + 1];
           4         for (int k = 0; k < S.length(); k++)
           5             f[k][0] = 1;
           6         for (int i = 1; i <= S.length(); i++) {
           7             for (int j = 1; j <= T.length(); j++) {
           8                 if (S.charAt(i - 1) == T.charAt(j - 1)) {
           9                     f[i][j] += f[i - 1][j] + f[i - 1][j - 1];
          10                 } else {
          11                     f[i][j] += f[i - 1][j];
          12                 }
          13             }
          14         }
          15         return f[S.length()][T.length()];
          16     }
          17 }

          Edit Distance
          Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
          You have the following 3 operations permitted on a word:
          a) Insert a character
          b) Delete a character
          c) Replace a character

           1 public class EditDistance {
           2     public int minDistance(String word1, String word2) {
           3         if (word1.length() == 0 || word2.length() == 0)
           4             return word1.length() == 0 ? word2.length() : word1.length();
           5         int[][] arr = new int[word2.length() + 1][word1.length() + 1];
           6         for (int i = 0; i <= word1.length(); i++) {
           7             arr[0][i] = i;
           8         }
           9         for (int j = 0; j <= word2.length(); j++) {
          10             arr[j][0] = j;
          11         }
          12         for (int i = 0; i < word1.length(); i++) {
          13             for (int j = 0; j < word2.length(); j++) {
          14                 if (word1.charAt(i) == word2.charAt(j)) {
          15                     arr[j + 1][i + 1] = arr[j][i];
          16                 } else {
          17                     int dis = Math.min(arr[j][i + 1], arr[j + 1][i]);
          18                     arr[j + 1][i + 1] = Math.min(arr[j][i], dis) + 1;
          19                 }
          20             }
          21         }
          22         return arr[word2.length()][word1.length()];
          23     }
          24 }
          posted on 2013-12-31 10:21 Meng Lee 閱讀(248) 評(píng)論(0)  編輯  收藏 所屬分類: Leetcode
          主站蜘蛛池模板: 南靖县| 浦江县| 兴义市| 苍梧县| 梁山县| 聂拉木县| 渝北区| 台山市| 法库县| 平南县| 海原县| 中卫市| 正定县| 东平县| 沅江市| 阳朔县| 水富县| 清镇市| 磐安县| 浮山县| 定西市| 河间市| 瓦房店市| 太原市| 南华县| 上蔡县| 辽中县| 旬邑县| 邵阳市| 长丰县| 乌什县| 威海市| 民乐县| 水城县| 鲜城| 龙游县| 甘谷县| 游戏| 成武县| 家居| 五台县|