Description

Given a string, find length of the longest repeating subsequence such that the two subsequence don’t have same string character at same position, i.e., any ith character in the two subsequences shouldn’t have the same index in the original string.

Example

Example 1:

Input:"aab"
Output:1
Explanation:
The two subsequence are a(first) and a(second).
Note that b cannot be considered as part of subsequence as it would be at same index in both.

Example 2:

Input:"abc"
Output:0
Explanation:
There is no repeating subsequence

思路:动态规划。
dp[i][j] 代表前i个与前j个匹配的最大长度。
若字符相同而位置不同,即可转移。
public class Solution {
/**
* @param str: a string
* @return: the length of the longest repeating subsequence
*/
public int longestRepeatingSubsequence(String str) {
int n = str.length(); int[][] dp = new int[n + 1][n + 1]; for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
dp[i][j] = 0; for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (str.charAt(i - 1) == str.charAt(j - 1) && i != j)
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
}
}
return dp[n][n];
}
}

  

Longest Repeating Subsequence的更多相关文章

  1. [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  2. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  3. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  4. [Swift]LeetCode873. 最长的斐波那契子序列的长度 | Length of Longest Fibonacci Subsequence

    A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...

  5. 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...

  6. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  7. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  9. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

随机推荐

  1. centos将nginx服务设置为开机自动启动

    centos将nginx服务设置为开机自动启动 1.在/etc/init.d下创建文件nginx 发现原来就有并且配置就是默认的,而且是正确的 命令为: vim /etc/init.d/nginx 需 ...

  2. python 之 Django框架(模板系统、过滤器、simple_tag、inclusion_tag、Tags、母版、组件)

    12.35 Django模板系统 {{ }}和 {% %},变量相关的用{{}},逻辑相关的用{%%} app02/views: # 模板语言测试函数 def template_test(reques ...

  3. python3 最基础

    python 2乱码,源码混了,代码重复,ascii码 一个字节表示 显示中文 只有英文 python 3 utf-8 三个字节 表示中文 int 整型 str 字符串 类型 bool 布尔值 Tru ...

  4. leetcode 罗马数字和数字的互相转换

    不知哪个大佬说过: 关于字符串的题都可以用指针或哈希解决. 罗马数字转数字: 思想: 我们能观察到规律: 一般情况下,表示大的字母在前,小字母在后; 特殊情况下,小字母会在大字母之前,但是相应的,得到 ...

  5. git简单介绍

    一种常见的版本控制工具 获取 克隆仓库 git支持以ssh或者http的方式来标识远程仓库 git clone git@github.com:username/project.git git clon ...

  6. golang ---CPU信息

    package main import ( "fmt" "github.com/StackExchange/wmi" ) type gpuInfo struct ...

  7. NEST routing timeout scroll

    /// <summary> /// PUT /employee/employee/9e5e50da-7740-488e-bee2-b24951435691?routing=test_rou ...

  8. DIY一个Web框架

    一.前言 二.框架结构及实现流程 三.总结 一.前言 当我们了解了Web应用和Web框架,以及HTTP协议的原理之后,我们可以自己动手DIY一个最简单的WEB框架,以加深对Web框架的理解,并为即将学 ...

  9. Matlab函数装饰器

    info.m function result_func= info(msg) function res_func =wrap(func) function varargout = inner_wrap ...

  10. centos下安装nginx(转载)

    http://blog.csdn.net/u010246789/article/details/51501710 有声明,不能转载,所以,就把地址弄了过来