Cracking the coding interview
Arrays and String solutions
Hey guys i am providing the solutions for the question given in the Chapter one Arrays and String of the very famous book Cracking the coding interview. All these solutions are written by myself and checked for all the possible cases. Please take a look at them and help your self to solve your problems. If you have any query regarding the solutions or you want to ask something leave your comment and i will get back to you as soon as possible. Happy coding.
Question 3 from the Cracking the coding interview.
Write a method to replace all spaces in a string with '%20. Assume that the string has sufficient space at the end to hold the additional characters, and that you are given the true length of the string. ( If implementing in Java, you can use a character array so that you can perform this operation in place.) or you can use string builder.
EXAMPLEInput: "Mr John Smith "J 13
Output: "Mr%20John%20Smith"
Solution
You can find the solution for this problem here.
Solution 1 using java (Worst case Complexity N).
This is very simple problem if we talk about java implementation as java provide us String builder class which dynamically append characters to string.so we just need to check if the char in the original string is a blank space or not. if it is blank space then append the following text in the place of blank space and move on in the iteration. that is the simple logic behind this solution that i have followed.if you find this code hard to understand then please start writing your own code and use pen n paper for that so then you can keep track what is your code all about and what it is calculating.
This problem is one of the most asked question in the interviews so try ti understand this throughly and try similar problems like it.
import java.util.*; import java.lang.*; import java.io.*; class Ideone { public static void main (String[] args) throws java.lang.Exception { String str ="asd fg hj"; StringBuilder strb = new StringBuilder(); for(int i=0;i<str.length();i++) { if(str.charAt(i)==' ') { strb.append("%20"); } else { strb.append(str.charAt(i)); } } System.out.println(strb); } }
There are many other solution for this problem such as using string instead of string builder but it will consume more memory and you have to write extra line of code.You can traverse from the end of the string and add %20 to the string also.
Note that this approach is not only specific to this type of question you can use this sorting technique in various questions such as if you want to check whether to word are anagrams of one another or not. You can simply sort them and compare them to find out. This approach is also helpful in other questions so i suggest that its better to learn this tricks and keep in mind while attempting the question you may find it useful. You can always search for more coding problems like this to get comfortable to this to approach or you can find new problems where this approach is applicable. So go and search more problems like this and check whether this approach is useful or not and you can tell me about it by commenting and writing about this to me.
Try more and more problems that you find on string and arrays and try to develop an approach the will help you understand problems rather then learning the solution. Once you know how to approach the problem you will able to solve any problem related to strings and array. I wish best of luck to you for your future. I wish best of luck to you for your future.
0 comments:
Post a Comment