Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, July 12, 2017

Reverse the string InterviewBit Solution


Idea is to split string whenever a white space is detected , concat this arr in reverse manner to a string and return string without last blank space.


public class Solution {
public String reverseWords(String a) {
   String arr[]=a.split("\\s");
   String str=new String();
   for(int i=arr.length-1;i>=0;i--)
{   str=str.concat(arr[i]);
   str=str.concat(" ");
}
   return str.trim();  
}
}

0 Comments:

Post a Comment

Stats