1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// write a method to replace all whites spaces in a string with 20%
// new York --> new%20York20%20%
// Date: March 15, 2016
// Desc:  Interview for Sony technical interview

import java.util.*;

public class SonyReplaceWhiteSpace {
    public static void main(String[] args) {
        String input = "new York";
        System.out.printf("result = %s", replaceWhiteSpace(input));
    }
    
    public static String replaceWhiteSpace( String str) {
        String result = "";
        
        if (str == null || str.length() == 0)  return result;
        
        int n = str.length();
        HashMap<Integer, String> map = new HashMap<>();

        char[] arr = str.toCharArray();

         for (int i = n -1 ; i >= 0 ; i-- ) {
           if (arr[i] == ' ')  {
              String temp = "%20";
                 map.put(i, temp);
            }  else {
                map.put(i, String.valueOf(arr[i]));
            }

        }

        for ( String val : map.values()) {
            result += val;
        }
        //result = str.replaceAll(" ", "%20");
        return result;
    }
}