-
Title: Given a string and its substring S1 S2, S2 to find the first position S1.
-
Example:
示例1: 输入:S1 = “abcadcdvfcdg" S2 = "cd" 输出:5 示例2: 输入:S1 = “cdhbvdjhbvf" S2 = "bv" 输出:3
-
Code
import java.util.Scanner; class Solution { public static void main(String [] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String s2 = in.nextLine(); int n = 0; for (int i = s2.length(); i < s1.length(); i++) { if (s2.equals(s1.substring(n, i))) { System.out.println(n); break; } n++; } } }
-
operation result
abcadcdvfcdg cd 5
Given a string and its substring S1 S2, S2 to find a first location S1,
Guess you like
Origin blog.csdn.net/qq_44837912/article/details/104674706
Recommended
Ranking