class Solution { public String truncateSentence(String s, int k) { String ans=""; int n = s.length(); for(int i=0;i < n;i++) { if(s.charAt(i)==' ') { k--; if(k == 0) { break; } } ans+=s.charAt(i); } return ans; } }