字符串查找字符出现次数

Description:

描述:

It's a popular interview question based of dynamic programming which has been already featured in Accolite, Amazon.

这是一个流行的基于动态编程的面试问题,已经在亚马逊的Accolite中得到了体现。

Problem statement:

问题陈述:

Given two strings S and T, find the number of times the second string occurs in the first string, whether continuous or discontinuous as subsequence.

给定两个字符串ST ,找出第二个字符串在第一个字符串中出现的次数,无论是连续的还是不连续的作为子序列。

    Input:
String S: "iloveincludehelp"
String T: "il"
Output:
5

Explanation:

说明:

The first string is,

第一个字符串是

The second string is "il"

第二个字符串是“ il”

First occurrence:

第一次出现:

Second occurrence:

第二次出现:

Third occurrence:

第三次出现:

Fouth occurrence:

发生口:

Fifth occurrence:

第五次出现:

So, total distinct occurrences are 5.

因此,总的不重复发生次数为5。

Solution Approach:

解决方法:

First, we discuss the recursive solution and then we will convert it to dynamic programming.

首先,我们讨论递归解决方案,然后将其转换为动态编程。

Prerequisite:

先决条件:

    string s: the first string
string t: the second string
starts: start point of the first string
srartt: start point of the second string
m : length of first string
n : length of second string

How, how can we generate a recursive relation?

如何,如何生成递归关系?

Say,

说,

    starts=i where i<m and i>=0 & start=j where j<n and j>=0

Say,

说,

  1. s[starts] = t[start] that means both have same character,

    s [starts] = t [start]表示两个字符相同,

    Now we have to option,

    现在我们必须选择

    1. Check for starts+1, startt+1 which means we are looking for the same occurrence, we want to check for other characters as well.
    2. 检查starts + 1 , startt + 1 ,这意味着我们正在寻找相同的事件,我们也想检查其他字符。
    3. Check for starts+1, startt which means we are looking for another different occurrence.
    4. 检查starts + 1和startt ,这意味着我们正在寻找另一个不同的事件。
  2. s[starts] != t[start]

    s [开始]!= t [开始]

    Now we have only one option which is check for

    现在我们只有一个选项可以检查

    starts+1, startt as we need to look for different occurrence only.

    starts + 1 , startt,因为我们只需要查找不同的事件。

    Function distinctOccurence(string s,string t,int starts,int startt,int m,int n)
if startt==n   //enter substring is matched
return 1;
if starts==m         //enter string has been searched with out match
return 0;
if(s[starts]!=t[startt])
//only one option as we discussed
return distinctOccurence(s,t,starts+1,startt,m,n);
else
//both the options as we discussed
return distinctOccurence(s,t,starts+1,startt+1,m,n) + distinctOccurence(s,t,starts+1,startt,m,n);

The above recursion will generate many overlapping subproblems and hence we need to use dynamic programming. (I would recommend to take two short string and try doing by your hand and draw the recursion tree to understand how recursion is working).

上面的递归将产生许多重叠的子问题,因此我们需要使用动态编程。 (我建议您取两个短字符串,然后用手尝试画出递归树,以了解递归的工作方式)。

Let's convert the recursion to DP.

让我们将递归转换为DP。

    Step1: initialize DP table
int dp[m+1][n+1];
Step2: convert step1 of recursive function
for i=0 to n
dp[0][i]=0;
Step3: convert step2 of recursive function
for i=0 to m
dp[i][0]=1;
Step4:   Fill the DP table which is similar to step3 of the recursion function
for i=1 to m
for j=1 to n
if s[i-1]==t[j-1]
dp[i][j]=dp[i-1][j]+dp[i-1][j-1]
else
dp[i][j]=dp[i-1][j]
end for
end for
Step5: return dp[m][n] which is the result.

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int distinctOccurence(string s, string t, int starts, int startt, int m, int n) {//note argument k,l  are of no use here
//initialize dp table
int dp[m + 1][n + 1];
//base cases
for (int i = 0; i <= n; i++)
dp[0][i] = 0;
for (int i = 0; i <= m; i++)
dp[i][0] = 1;
//fill the dp table
for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (s[i - 1] == t[j - 1])
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[m][n];
}
int main() {int n, m;
string s1, s2;
cout << "Enter the main string:\n";
cin >> s1;
cout << "Enter the substring:\n";
cin >> s2;
m = s1.length();
n = s2.length();
cout << s2 << " has " << distinctOccurence(s1, s2, 0, 0, m, n) << " times different occurences in " << s1 << endl;
return 0;
}

Output

输出量

Enter the main string:
iloveincludehelp
Enter the substring:
il
il has 5 times different occurences in iloveincludehelp

翻译自: https://www.includehelp.com/icp/find-number-of-times-a-string-occurs-as-a-subsequence.aspx

字符串查找字符出现次数

字符串查找字符出现次数_查找字符串作为子序列出现的次数相关推荐

  1. 统计并输出某给定字符在给定字符串中出现的次数_查找常用字符

    给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 ...

  2. c语言 字符串字符反向储存_反向字符串的PL / SQL程序

    c语言 字符串字符反向储存 Here you will get pl/sql program to reverse a string. 在这里,您将获得pl / sql程序来反转字符串. The su ...

  3. python中字母是什么类型_Python中只有一个字母的字符串属于字符类型。( )_学小易找答案...

    [单选题]下列选项中,用于标识为静态方法的是( ). [单选题]"多.夺.躲.惰"的区别在于( ) (7.0分) [单选题]子类能继承父类的一切属性和方法.( ) [单选题]使用类 ...

  4. string 字符串中字符无效_7.3 C++字符串类 | 使用string输出

    C++字符串类 C++提供了一种新的数据类型:字符串类型,它和char,int类型一样,可以用来定义变量,用一个名字代表一个字符序列,这就是字符串变量. 读者需要明白string并不是C++本身具有的 ...

  5. 字符串某个字符修改颜色、给字符串添加

    字符串某个字符修改颜色: UILabel *titleLabel = [[UILabel alloc]init]; titleLabel.font = smallFont; titleLabel.te ...

  6. java 统计字符串中字符个数_java实现统计字符串中字符及子字符串个数的方法示例...

    本文实例讲述了java实现统计字符串中字符及子字符串个数的方法.分享给大家供大家参考,具体如下: 这里用java实现统计字符串中的字符(包括数字.大写字母.小写字母以及其他字符)个数,以及字符串的子字 ...

  7. JavaScript 获取字符串指定字符的数量 JS 获取字符串指定字符的数量

    JavaScript 获取字符串指定字符 //循环对比计数 效率最低 function getCharCount1(str,char){let count= 0;for(let i=0;i<st ...

  8. C语言——字符串和字符数组的区别及字符串的赋值

    几点区别: 1.一维数组中不一定存放字符串,但字符串一定要存放在一维数组里. 2.字符数组的每一个元素可存放一个字符,但它并不限定最后一个字应该是什么. 3.不可以用赋值语句将字符串常量或其他字符串赋 ...

  9. java gc 次数_浅谈如何减少GC的次数

    GC会stop the world.会暂停程序的执行,带来延迟的代价.所以在开发中,我们不希望GC的次数过多. 本文将讨论如何在开发中改善各种细节,从而减少GC的次数. (1)对象不用时最好显式置为 ...

最新文章

  1. 服务容错保护断路器Hystrix之二:Hystrix工作流程解析
  2. 函数作为变量,类型---golang
  3. android library依赖 aar_Android 合并AAR踩坑之旅
  4. Linux 下 ls -l 命令执行显示结果的每一列含义 图解 (附带ls命令详解)
  5. 击中-击不中变换(约束)—lhMorpHMTC
  6. figma下载_Figma重新构想的团队库
  7. Linux基本管理篇
  8. 关于DOM的事件操作
  9. window 清理maven本地仓库
  10. vue.js中在js获取指定日期到现在时间的天数
  11. 实验2014051901:opencv操作摄像头
  12. jvm参数调优_3_问题排查
  13. 支付系统就该这么设计(万能通用)
  14. OpenStack组件部署之Placement
  15. DeepLearning | 图卷积神经网络(GCN)解析(论文、算法、代码)
  16. 急刹车或停车时应该先踩离合还是先踩刹车?
  17. 自动驾驶传感器产业链
  18. Android 一种关于解决 No view found for id xxxx for fragment xxxx 问题的方案
  19. char *str 和 char str[] 的区别
  20. 欧姆龙, PLC CJ2M标准程序,一共控制12个伺服电机 ,气缸若干,包含轴点动,回零

热门文章

  1. php绘制频谱图,一步一步教你实现iOS音频频谱动画(二)
  2. 删除单元格_VBA(实验1)用VBA 删除某列空单元格的3种方法:删除法,转移到其他列方法,数组方法...
  3. ios 请求失败封装_vue_axios请求封装、异常拦截统一处理
  4. java 流式_Java开发笔记(七十二)Java8新增的流式处理
  5. 语言爬虫字段为空_我为什么建议前端将Python 作为第二语言?
  6. Redis(二):Redis入门与性能测试
  7. 移动Web加速技术月报第1期
  8. Monthly Expense【二分】
  9. Find the Difference(leetcode389)
  10. 《iOS 6核心开发手册(第4版)》——2.11节秘诀:构建星星滑块