
Will the program behave differently if you change the y++ to ++y? Why or why not.ĭoes the array copy at the end represent another 'N' pass through the entire array making runtime complexity O(n*n) instead of O(n) ? Why or why not.Ĭan you replace the double equals comparing primitive characters with a. Talking points exercise for coder interviews: When the index x that we are peeking at is not represented in our repository, then we pull that one and add it to the end of our repository at index tail and increment tail.Īt the end, we return the array between the points 0 and tail, which should be smaller or equal to in length to the original array. When a duplicate is found, it breaks out and quits, the y=tail returns false and the repository is not contributed to. We define the variable y outside of the loop because we want to find the first location where the array index that we are looking at has been duplicated in our repository. At the beginning of the function the answer is: "the characters between 0 and 1" as between 0 and tail. The first part of the array passed in is used as the repository for the unique characters that are ultimately returned.

#DELETE DUPLICACY IN STRING IN JAVA HOW TO#
How to read and talk about the above code: ("Program took: " + duration + " nanoseconds") Public static char removeDupes(char arr))) Īssert "ab".equals(result) : "abba should return ab but it returns: " + result An extra array is not: import java.util.* String s'Bangalore-Chennai-NewYork-' using Java. String s'Bangalore-Chennai-NewYork-Bangalore-Chennai' and output should be like. NOTE: One or two additional variables are fine. Can anyone please let me know how to remove duplicate values from. If (rem_str.length() = 0 & last_removed = str.Code to remove the duplicate characters in a string without using any additional buffer. Return rem_str.substring(1,rem_str.length()) String rem_str = removeUtil(str.substring(1,str.length()), last_removed) While (str.length() > 1 & str.charAt(0) = str.charAt(1)) Static String removeUtil(String str, char last_removed) In this way we are exploiting the LIFO property of the stack.Now add the contents of the stack onto new string and reverse it to preserve the order.Below solution explains it better. Iterate through the string from left to right,if stack is empty put the current character onto the stack.Įlse check if the top of the stack equals the current element then pop the top element from the stack, if not push the current element. This is the standard stack problem.Let’s see how stack works here. Recursively remove all adjacent duplicates in the string till no duplicates is left in the string. 'c',we pop from stack as the current element matches the top of stack. The result of this move is that the string is "bbdb", of which only "bb" is possible, so the final string is "db".

We can remove "cc" since the letters are adjacent and equal, and this is the only possible move. See original problem statement here Example: Input: We repeatedly make duplicate removals on str until we no longer can.


Given a string str of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
