Is this a smart method?public static ArrayList<Integer> getWordLengths( String s ){    String str =   + s +  ;    ArrayList<Integer> list = new ArrayList<Integer>();    for ( int i = 0; i < str.length(); i++ )    {        if ( str.charAt(i) == ' ' )        {            for ( int j = i+1; j < str.length() ; j++)            {                if ( str.charAt(j) == ' ')                {                    list.add( j - i - 1 );                    j = str.length();                }            }        }    }    return list;        }Sample output:System.out.println( getWordLengths(This is really easy)); [4, 2, 6, 4]so it worked.We are only allowed to use length and charAt from the String class.
Getting the lengths of each word in its String parameter
java;strings
Code:    String s = This is really easy;    int count = 0;    for (int i = 0; i < s.length(); i++) {        if (s.charAt(i) == ' ') {            System.out.println(count is  + count);            count = 0;        } else {            count++;        }    }    System.out.println(count is  + count);Output:count is 4count is 2count is 6count is 4Explanation:keep counting the length of each word till you see a blank. As soon as, you see a blank, write down a length current word and set count to zero and make it ready for new word. At the end, you should write down the length the last word outside of the for loop.Note: Big o of my code is n , yet your is n^2; therefore, my method is faster than your approach. 