Java String Tokens

  • + 0 comments

    Why it needs to trim()?

    If it doesn't trim() , it will not pass all the test even if they output the same thing

    Why?

    I would like to know

    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            scan.close();
            if (s.trim().length()==0 || s.trim().length()>400000){
            System.out.println(0);
            return;
            }
            s=s.trim();
            String regex = "[^(a-zA-Z)]+";
            String [] lista =s.split(regex);
            
           
            System.out.println(lista.length);
            
            
            for(String k:lista){
               System.out.println(k);
            }
            
            
            
        }
    }