查看: 1660| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[字符串] 狗家高频1055的解法没看懂

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
From any string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions).

Given two strings source and target, return the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, return -1.



Example 1:

Input: source = "abc", target = "abcbc"
Output: 2
Explanation: The target "abcbc" can be formed by "abc" and "bc", which are subsequences of source "abc".
Example 2:

Input: source = "abc", target = "acdbc"
Output: -1
Explanation: The target string cannot be constructed from the subsequences of source string due to the character "d" in target string.
Example 3:

Input: source = "xyz", target = "xzyxz"
Output: 3
Explanation: The target string can be constructed as follows "xz" + "y" + "xz".


Constraints:

Both the source and target strings consist of only lowercase English letters from "a"-"z".
The lengths of source and target string are between 1 and 1000.


  1. class Solution {
  2.     public int shortestWay(String source, String target) {
  3.         char[] sc = source.toCharArray(), ta = target.toCharArray();
  4.         boolean[] map = new boolean[26];
  5.         for (char c : sc) {
  6.             map[c - 'a'] = true;
  7.         }
  8.         
  9.         int j = 0, res = 1;
  10.         for (int i = 0; i < ta.length; i ++, j ++) {
  11.             if (!map[ta[i] - 'a']) return -1;
  12.             while (j < sc.length && sc[j] != ta[i]) j ++;
  13.             if (j == sc.length) {
  14.                 res ++;
  15.                 j = -1;
  16.                 i --;
  17.             }
  18.         }
  19.         return res;
  20.     }
  21. }
复制代码




Greedy

Use two pointers, one for source: i, one for target: j. While j scan through target, try to match each char of j in source by moving i. Count how many times i goes through source end.


  x y z
    i

  x z y x z
       j

比如这样的时候,为什么j就qualified的了?




上一篇:请教leetcode线程题1195
下一篇:为什么刷过的题还是不会写
全局:
xz + y + xz啊。j还是移动了的
回复

使用道具 举报

🔗
337845818 2019-11-1 03:38:52 | 只看该作者
全局:
一遍一遍扫呗有啥看不懂的, 你问题在哪

评分

参与人数 1大米 +1 收起 理由
dennyzhang007 + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
codeyy 2019-11-1 06:13:49 | 只看该作者
全局:
哈哈,我还只会暴力n方的解决法。

评分

参与人数 2大米 +3 收起 理由
九道 + 1 给你点个赞!
dennyzhang007 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
qazws 2019-11-1 14:07:57 | 只看该作者
全局:
codeyy 发表于 2019-11-1 06:13
哈哈,我还只会暴力n方的解决法。

上面给的解法就是n方,或者更准确的:n*m

为了做到O(n+m),需要将source改写为一个len(source)*26的二维数组,记录在source每个index处,下一个指定字符c在什么位置,这样在source上的iteration就是O(1)了
回复

使用道具 举报

🔗
onefly 2019-11-2 20:45:25 | 只看该作者
全局:
楼上说的对,其实就是类似 KMP 的 next 数组
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表