🔥 10.14 360笔试面经 - 编程题 & 题解

alt

考试平台: 牛客

考试时间: 2023-10-14 (120 分钟)

考试题型: 选择题 + 2道编程题

投递岗位:大数据开发工程师

T1 复制粘贴

时间限制:3000MS

内存限制:589824KB

题目描述

小明最近学会了快捷键Ctrl C、Ctrl V。具体来说,给她一个字符串,她复制其中的一个字符并将其粘贴到这个字符的下一位,从而整个字符串的长度加一。小树来检测她的学习成果。他给出两个字符串 s、t,小明可以对 s 进行任意次复制粘贴操作,请问她能否将 s 转化成 t?

输入描述

本题采用多组数据测试,第一行为数据组数T。

在接下来的每组数据中 (1<=T<=100) 第一行有个非空字符串 s,第二行有一个非空字符串t。

字符串中均为小写字母,所有测试数据的 s 的长度之和与t的长度之和都不超过.

输出描述

对于每组数据输出一行,如果可以转化输出YES,否则输出NO.

样例

输入:
4
hello
hello
hello
helloo
hello
hlllloo
hello
helo

输出:
YES
YES
NO
NO

题解

双指针,

题意:复制其中的一个字符并将其粘贴到这个字符的下一位,意思就是 t 中的字符可以比s中的某个字符串重复的次数多,但不能少。

两个字符串利用两个指针进行模拟比较。

import java.util.Scanner;

/**
 * P1
 * @author code5bug (同v)
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        in.nextLine();

        Solution solution = new Solution();
        for (int i = 0; i < T; i++) {
            String s = in.nextLine();
            String t = in.nextLine();
            System.out.println(solution.solve(s.toCharArray(), t.toCharArray()) ? "YES" : "NO");
        }
    }
}


class Solution {
    public boolean solve(char[] s, char[] t) {
        if (s.length > t.length) return false;

        int n1 = s.length, n2 = t.length;
        int i = 0, j = 0;
        while (i < n1 && j < n2) {
            int cnt1 = 0;
            while (i + 1 < n1 && s[i] == s

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

🔥笔试编程真题宝典💯 文章被收录于专栏

📕分享大厂机试真题深度剖析核心考点,助你速通面试。

全部评论
Solution没看懂,有人能解释一下吗
点赞 回复 分享
发布于 2023-10-17 15:29 山东

相关推荐

07-29 14:27
门头沟学院 Java
LMQICU:大暴雨阁下真的没绷住
点赞 评论 收藏
分享
Gaynes:查看图片
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务