sweet1cris

Untitled

Mar 6th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1.  
  2.   // Method 1: Not using any String/StringBuilder utility,
  3.   // and using char[] to do it "in place".
  4.   public String replaceI(String input, String s, String t) {
  5.     // Assumptions: input, s, t are not null, s is not empty.
  6.     char[] array = input.toCharArray();
  7.     if (s.length() >= t.length()) {
  8.       return replaceShorter(array, s, t);
  9.     }
  10.     return replaceLonger(array, s, t);
  11.   }
  12.  
  13.   public String replaceShorter(char[] input, String s, String t) {
  14.     // We reuse the input char array since the number of characters needed is
  15.     // less.
  16.     // fast and slow pointers both from left to right direction.
  17.     int slow = 0;
  18.     int fast = 0;
  19.     while (fast < input.length) {
  20.       // when we find a match of s on the substring starting from the fast
  21.       // pointer
  22.       // we copy the t at slow pointer.
  23.       if (fast <= input.length - s.length() && equalSubstring(input, fast, s)) {
  24.         copySubstring(input, slow, t);
  25.         slow += t.length();
  26.         fast += s.length();
  27.       } else {
  28.         input[slow++] = input[fast++];
  29.       }
  30.     }
  31.     return new String(input, 0, slow);
  32.   }
  33.  
  34.   public String replaceLonger(char[] input, String s, String t) {
  35.     // Notice: we will need a longer array in the case, and if the requirement
  36.     // is "in place", usually you can assume you are given a long enough
  37.     // char array already, and the original input string resides
  38.     // on part of the char array starting from index 0.
  39.     // In this solution, we actually allocate a larger array on demand, and the
  40.     // purpose of the solution is to demonstrate how to do it "in place".
  41.  
  42.     // get all the matches end positions in the input char array of string s.
  43.     ArrayList<Integer> matches = getAllMatches(input, s);
  44.     // calculate the new length needed.
  45.     char[] result = new char[input.length + matches.size() * (t.length() -s.length())];
  46.  
  47.  
  48.     // slow and fast pointers both from right to left direction.
  49.     // slow: the position when traversing the new length.
  50.     // fast: the position when traversing the old length.
  51.     // lastIndex: the rightmost matching end position’s index.
  52.     int lastIndex = matches.size() - 1;
  53.     int fast = input.length - 1;
  54.     int slow = result.length - 1;
  55.     while (fast >= 0) {
  56.       // only if we still have some match and fast is in the position of
  57.       // rightmost matching end position, we should copy t.
  58.       if (lastIndex >= 0 && fast == matches.get(lastIndex)) {
  59.         copySubstring(result, slow - t.length() + 1, t);
  60.         slow -= t.length();
  61.         fast -= s.length();
  62.         lastIndex--;
  63.       } else {
  64.         result[slow--] = input[fast--];
  65.       }
  66.     }
  67.     return new String(result);
  68.   }
  69.  
  70.   // check if the substring from fromIndex is the same as s.
  71.   private boolean equalSubstring(char[] input, int fromIndex, String s) {
  72.     for (int i = 0; i < s.length(); i++) {
  73.       if (input[fromIndex + i] != s.charAt(i)) {
  74.         return false;
  75.       }
  76.     }
  77.     return true;
  78.   }
  79.  
  80.   // copy the string t to result at fromIndex.
  81.   private void copySubstring(char[] result, int fromIndex, String t) {
  82.     for (int i = 0; i < t.length(); i++) {
  83.       result[fromIndex + i] = t.charAt(i);
  84.     }
  85.   }
  86.  
  87.  
  88.   // get all the matches of s end positions in input.
  89.   private ArrayList<Integer> getAllMatches(char[] input, String s) {
  90.     ArrayList<Integer> matches = new ArrayList<Integer>();
  91.     int i = 0;
  92.     while (i <= input.length - s.length()) {
  93.       if (equalSubstring(input, i, s)) {
  94.         // we record the match substring's end index instead of start index,
  95.         // for later convenience.
  96.         matches.add(i + s.length() - 1);
  97.         i += s.length();
  98.       } else {
  99.         i++;
  100.       }
  101.     }
  102.     return matches;
  103.   }
Advertisement
Add Comment
Please, Sign In to add comment