Skip to main content

[Interview] Check Permutation

[Interview] Check Permutation



--------------------------------------------------------------------------------------------------------------------------


Question:
Given 2 Strings, write a method to decide if one is a permutation of the other.

--------------------------------------------------------------------------------------------------------------------------

Idea 1: 
count the number of each character in first string and check if the number of each character in the 2nd string is the same as the 1st string.

Solution 1:
public class Solution{
 boolean checker(String str1, String str2)
 {
  if (str1.length() != str2.length()) return false;
  
  int[] helper = new int[256];
  
  for(int i = 0 ; i<str1.length();i++)
  {
   int index = str1.charAt(i);
   helper[index]++;
  }
  
  for(int i = 0 ; i<str2.length();i++)
  {
   int index = str2.charAt(i);
   helper[index]--;
   if(helper[index]<0) return false;
  }
  
  return true;
 }
}

--------------------------------------------------------------------------------------------------------------------------

Idea 2: 
Sort the strings and check if they are same

Solution 2:
public class Solution{
 boolean checker2(String str1, String str2)
 {
  if (str1.length() != str2.length()) return false;
  return sort(str1).equals(sort(str2));
 }
 
 String sort(String str)
 {
  char[] c = str.toCharArray();
  Arrays.sort(c);
  return new String(c);
  
 }

}
--------------------------------------------------------------------------------------------------------------------------
Video Explanation






Comments

Popular posts from this blog

[LeetCode Solution 230]: Kth Smallest Element in a BST

Question: Given a binary search tree, write a function  kthSmallest  to find the  k th smallest element in it. ************************************************************************************************************************************ Write Infront To read to a tutorial, please to read the tutorial of in-order traversal of BST, please check: LeetCode Solution 94: Binary Tree Inorder Traversal We are going to solve this question using the following 4 methods: ->Binary Search ->Recursive ->Iterative ->Morris  Approach #1 Binary Search [Accepted] Detail Explanation The first method to solve this problem is using Binary Search. The idea is very easy and extremely to think. We use BST's property that the left child of the root is smaller than the root while the right child of the root is always bigger. We consider that the root is the pivot, and find the number of the nodes in the left subtree and the number of the nodes in th

[LeetCode Solution 145] Binary Tree Postorder Traversal

[LeetCode Solution 145]: Binary Tree Postorder Traversal Question: Given a binary tree, return the  postorder  traversal of its nodes' values. For example: Given binary tree  {1,#,2,3} , 1 \ 2 / 3 return  [3,2,1] . Approach #1 Recursive [Accepted] Detail Explanation The first method to solve this problem is using recursive. This is the classical method and straightforward. we can define a helper function to implement recursion. The java code is as follows: Java public class Solution { public List<Integer> postorderTraversal (TreeNode root) { List<Integer> res = new ArrayList<>(); helper(root, res); return res; } public void helper (TreeNode root, List<Integer> res) { if (root != null ) { if (root.left != null ) { helper(root.left, res); } if (root.right != null ) { helper(root.right, res); } res.add(root.val); } } } Complexity Analysis Time complexity :

[Interview]: URLify

[Interview]  URLify: -------------------------------------------------------------------------------------------------------------------------- Question: URLify: Write a method to replace all spaces in a string with ‘%20’, you may assume that the string has sufficient space at the end to hold the additional characters. Example  input: ' mr john smith '  output: ' mr %20john%20smith' --------------------------------------------------------------------------------------------------------------------------   Idea 1:  Start from the back and start replacing until the character is not ' ', and replace the characters in reverse order. Solution 1: public class Solution{ public String replace(char[] str) { boolean flag = false; StringBuffer sb = new StringBuffer(); for (int i = str.length - 1; i >= 0; i--) { if (str[i] != ' ') flag = true; if (flag == true) { if (str[i] == ' ') { s