2

What does this definition of contiguous subsequences mean?

I'm working in a problem but I don't understand how contiguous subsequences works. For example,

Finding all 3 character length substrings in a string

public class Subsequences {

    public static void main(String[] args) {

        String s = "CCAATA CCGT";
        String ss = s.replaceAll("\\s+","");

        int n = 4; // subsequences of length

        for (int i=0; i <= ss.length() - n; i++) {
             String substr = ss.substring(i, i + n);
             if (substr.matches("[a-zA-Z]+")) { 
                 System.out.println(substr); 
                }
        }
    }
}

Output:

CCAA CAAT AATA ATAC TACC ACCG CCGT

Would anyone explain to me what this loop do?

for (int i=0; i <= ss.length() - n; i++) {
                 String substr = ss.substring(i, i + n);
                 if (substr.matches("[a-zA-Z]+")) { 
                     System.out.println(substr); 
                    }
            }
CaptainMar
  • 69
  • 7
  • 4
    What exactly about the loop don't you understand? What do you understand? – C-Otto Jan 22 '18 at 13:24
  • 1
    have you tried to debug it? – Nahuel Fouilleul Jan 22 '18 at 13:27
  • If your given sequence is ```CCAATACCGT```, then you can form contiguous (uninterrupted) subsequences of length 4 just by taking 4 consecutive letters from the sequence. For example, starting from index 0 subsequence is ```CCAA```, starting from index 1 subsequence is ```CAAT```, starting from index 2 subsequence is ```AATA``` and so on. – TheJavaGuy-Ivan Milosavljević Jan 22 '18 at 13:27
  • Why do we skip a character each time. "CCAATACCGT" = CCAA, CAAT, AATA, ATAC? – CaptainMar Jan 22 '18 at 13:29
  • I'm lost with this https://stackoverflow.com/a/22148898/9251579 – CaptainMar Jan 22 '18 at 13:32
  • To get all the possibilities you have for taking four (4) consecutive (one immediately following the other) characters from a string you need to start with the first character and take four, then proceed to the second and take another four, and so on, until you are too close to the end. – laune Jan 22 '18 at 13:38
  • Thanks all! I did it by hand and things got much clearer. – CaptainMar Jan 22 '18 at 14:04

1 Answers1

0

It ensures that the substring contains only letters (no spaces)

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24