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 4
Given a string, write a function to check if it is a permutation of a palindrome. Palindrome is a word that is the same forwards and backwards. A permutation is a rearrangement of letters. and remember that the palindrome does not need to be limited to just dictionary words there can be many more possible combinations.
EXAMPLE
Input: Tact Coa
Output: True ("taco cat". "atco cta".) means that they are palindrome permutation.
Solution
You can find the solution for this problem here.
Solution 1 using java .
Palindrome is the basic term that all programmers or who want to be become a programmer should know about it. Palindrome are basically those word that have same spelling when checked forward and backward here is the example NITIN is a palindrome means that we can write forward and backward it still be spelled NITIN. Remember this always as you will be facing many problem that have palindrome involved in it in one way or another. You can search more about palindrome and practice many more problems related to palindrome so that you become familiar with palindrome.
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 thoroughly 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 { System.out.println(permutation()); } public static boolean permutation() { String a="tacocat"; String b="atcocta"; int count=0; int[] arr_a = new int[256]; int[] arr_b = new int[256]; if(a.length()!=b.length()) { return false; } for(int i=0;i<a.length();i++) { arr_a[(int)(a.charAt(i))]++; arr_b[(int)(b.charAt(i))]++; } for(int i=0;i<a.length();i++) { if(arr_a[(int)(a.charAt(i))]!=arr_b[(int)(a.charAt(i))]) { return false; } if(arr_a[(int)(a.charAt(i))]==1) { count++; } } if(count>1) { return false; } return true; } }
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.
0 comments:
Post a Comment