简体   繁体   中英

How to replace a specific character in an array with two characters

So I just came back from a job interview and one of the questions I had to face with was:

"Given an array of characters and three characters for example:

Array: [a,b,c,z,s,w,y,z,o]

Char 1: 'z'

Char 2: 'R' Char 3: 'R'

Your goal is to replace each 'z' in the array to become two R characters within O(N) time complexity.

so your input will be Array: [a,b,c,z,s,w,y,z,o]

and your output array will be: [a,b,c,R,R,s,w,y,R,R,o]

assume that there is no 'R' in the array before.

You are not allowed to use other arrays or other variables.

The algorithm should be in-line algorithm.

Your final array must be a characters array."

My solution was within O(N^2) time complexity but there is a solution within O(N) time complexity.

The interview is over but I am still thinking about this problem, Can anyone help me to solve this?

First scan the input to count how many occurrences of char 1 exist. This has a linear time complexity.

From that you know that the length of the final array will be the input length + the number of occurrences.

Then extend the array to its new length, leaving the new slots empty (or whatever value). The exact nature of the operation depends on how the array data structure is implemented. This can surely be done with at worst a linear time complexity.

Use two indexes, i and j , where i references the last character of the input array and j references the very last index in the array (potentially to an empty slot).

Start copying from i to j each time decreasing the values of these indices with one. If you copy the matching letter, then duplicate the copied character to j again, and only reduce j . This has again a linear time complexity.

The algorithm will end with both i and j equal to -1.

Do two iterations.

First, count the number of char1 s ('z' in your example).
Now you know how long your array should be at the end: array.size() + num_char1s

Then, go from last to first with input and output iterators. If the element is char1 , insert to the end iterator the new chars, otherwise - just copy.

Pseudo code:

num_char1s = 0
for x in array:
  if x == char1:
    num_char1s++

// Assuming array has sufficient memory already allocated.
out_iterator = num_char1s + size - 1
in_iterator = size - 1

while (in_iterator >= 0):
  if (array[in_iterator] == char1):
    array[out_iterator--] = char3
    array[out_iterator--] = char2
  else:
    array[out_iterator--] = array[in_iterator]
  in_iterator--

In your question, two things are very important.

  1. can't use new variable
  2. can't use new array

So, we must need to use given array.

  1. First we will increase our given array size double. why? Cause at most our new array size = given_array_size*2 (if all characters = char 1)
  2. Now we will shift our given array n times right , where n= given_array_size.
  3. Now we will iterate our array from the new shifted position = n. iterate i=n to 2*n-1
  4. We will take j=0, which will write new array. if we found char 1 , we will make array[j++]=char 2 and array[j++]=char 3.
  5. But if a character is not 'z', we simply don't do anything. array[j++]=array[i]
  6. At last 0 to j-1 is the right answer.

Complexity: O(n)
No new variable and array needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM