【发布时间】:2017-03-14 13:57:37
【问题描述】:
我正在尝试解决一个问题。但我错过了一些极端情况。请帮我。问题陈述是:
您有一个字符串 S ,由小写英文字母组成。你可以对S进行两种操作:
- 在字符串末尾附加一个小写英文字母。
- 删除字符串中的最后一个字符。对空字符串执行此操作会产生空字符串。
给定一个整数k和两个字符串s和t,判断是否可以通过对上述操作执行exactly k将s转换为t s.
如果可能,打印 Yes;否则,打印编号。
Examples
Input Output
hackerhappy Yes
hackerrank
9
5 delete operations (h,a,p,p,y) and 4 append operations (r,a,n,k)
aba Yes
aba
7
4 delete operations (delete on empty = empty) and 3 append operations
我尝试过这种方式(C语言):
int sl = strlen(s); int tl = strlen(t); int diffi=0;
int i;
for(i=0;s[i]&&t[i]&&s[i]==t[i];i++); //going till matching
diffi=i;
((sl-diffi+tl-diffi<=k)||(sl+tl<=k))?printf("Yes"):printf("No");
请帮我解决这个问题。
谢谢
【问题讨论】:
-
那么你的问题是什么?你的算法有效吗?如果没有,请找一个反例。尝试您的算法 a) 在纸上手写或 b) 使用调试器找出算法出错的地方。
-
@MrSmith42,我试过了,但我错过了一些极端情况,我没有得到
-
“极端情况”的示例在哪里,调试的结果是什么?
-
它说 完全 k 个操作,所以你应该为 (abc,abd,3) 返回 FALSE
-
@MrSmith42,我正在在线编程平台上解决这个问题,我不知道测试用例。好的,我会尝试更多的测试用例,然后回来。