All-in-All: Niuke all-in-all

Source: Internet
Author: User

All-in-All: Niuke all-in-all
Question:

There are two strings s and t. If you delete some characters from s, connect the remaining characters to get t. T is the subsequence of s.
Develop a program to determine whether t is a subsequence of s.

Input description:

The input contains multiple groups of data. Each group of data contains two strings, s and t.

They all consist of numbers and letters and are less than 100000 in length.

Output description:

Corresponding to each input group. If t is a sub-sequence of s, "Yes" is output; otherwise, "No" is output ".

Input example:

ABC ABC
ABC AB
ABC DE

Output example:

Yes
Yes
No

Code:
package niuke;import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner read = new Scanner(System.in);        while(read.hasNext())        {            String str1 = read.next();            String str2 = read.next();            judgeStr(str1, str2);        }        read.close();    }    /**     *      * @param source     * @param target     * @return     */    public static int indexOf(char[] source, char[] target) {                int targetCount = target.length;        int sourceCount = source.length;                if (targetCount == 0) {            return 0;        }        char first = target[0];        int max = sourceCount - targetCount;        for (int i = 0; i <= max; i++) {            if (source[i] != first) {                while (++i <= max && source[i] != first)                    ;            }            if (i <= max) {                int j = i + 1;                int end = j + targetCount - 1;                for (int k = 1; j < end                        && source[j] == target[k]; j++, k++)                    ;                if (j == end) {                    return i;                }            }        }        return -1;    }        public static void judgeStr(String str1, String str2)    {        int len1 = str1.length(), len2 = str2.length();                int i = 0, j = 0;                for(; i<len1 && j<len2;)        {            if(str1.charAt(i) == str2.charAt(j))            {                j ++;            }            i ++;        }                if(j == len2)        {            System.out.println("Yes");        }        else        {            System.out.println("No");        }    }}

Note:

The question itself is not difficult, and the focus is on understanding the question purpose.

The indexOf (String, String) method in the Code indicates an incorrect understanding of this question, that is, it must be included in all to output Yes,

For example, "ABCD" "AB" outputs "Yes"

For "ABCD" "AD" output "No"

However, the question itself means that "Yes" must be output for "ABCD" "AD".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.