JAVA Novice Cow Knife Trial Extracts Substrings of Same Characters

Given a string and a substring signature, all substrings represented by the signature are required to be extracted from the source string, such as:

Given the source string: AaaBbb, and the substring signature ab, it is required to extract the substrings aa and bb consisting of all the same characters that match the signature

The JAVA code is as follows:

 1 import java.util.ArrayList;
 2 import java.util.Scanner;
 3 
 4 import static java.lang.System.*;
 5 import java.lang.String;
 6 public class FindSub
 7 {
 8     public static void main(String[] args)
 9     {
10         String src;
11         String sublable;
12         ArrayList<String> list=new ArrayList<String>();
13 
14         out.println("Please input the source string");
15         Scanner input=new Scanner(System.in);
16         src=input.nextLine();
17 
18         out.println("please input the lable of substring");
19         sublable=input.nextLine();
20 
21         list=getSub(src, sublable);
22 
23         out.println("All substring is:");
24         for(String s: list)
25         {
26             out.println(s);
27         }
28     }
29 
30     public static ArrayList<String> getSub(String src, String sublable)
31     {
32         int flag=0;
33         int low=0, high=0;
34         char sign='\u0000';
35 
36         ArrayList<String> list=new ArrayList<String>();
37 
38         for (int i=0; i<src.length(); i++)
39         {
40             if (sublable.contains(src.substring(i, i+1)))
41             {
42                 if (flag==0)
43                 {
44                     low=i;
45                     high=i;
46                     flag=1;
47                     sign=src.charAt(i);
48                 }
49                 else
50                 {
51                     if (src.charAt(i)==sign)
52                     {
53                         high=i;
54                     }
55                     else
56                     {
57                         list.add(src.substring(low, high+1));
58                         low=i;
59                         high=i;
60                         sign=src.charAt(i);
61                     }
62                 }
63             }
64             else
65             {
66                 if (flag==1)
67                 {
68                     list.add(src.substring(low, high+1));
69                     flag=0;
70                 }
71             }
72         }
73         if (flag==1)
74             list.add(src.substring(low, high+1));
75 
76         return list;
77     }
78 }

Run Results

This program can be improved to print the start and end labels of the substrings in the source string while outputting the extracted substrings, so that different substrings of the same feature tag can be distinguished, for example:

Source string AaaBaa signature tag a

Output aa 1, 2

aa 5,6

Lazy to change, interested in trying it yourself

Tags: Java

Posted on Sat, 04 Apr 2020 08:56:48 -0700 by Jamesm