Skip to main content

Posts

Showing posts with the label Interviews

[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

[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; } } ---------------------

[Interview] Reverse C-Style String

[Interview] Reverse C/C++ Style String  Question: Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.) Solution: public class Solution{ public String reverse(String str) { int p1 = 0; int p2 = str.length() - 1; char temp; char[] c = new char[p2]; c = str.toCharArray(); while (p1 < p2) { temp = c[p1]; c[p1] = c[p2]; c[p2] = temp; p1++; p2--; } return new String(c); } } Complexity Analysis: Time Complexity: \(O(n)\) Space Complexity: \(O(n)\)

[Interview] Check if String has Unique Characters

[Interview] Check if String has Unique Characters Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structure? package com.Array_Strings; /** * @Question: 1-1. Implement an algorithm to determine if a string has all * unique characters What if you can not use additional data * structure? */ public class Ans1_1 { public boolean helper(String str) { boolean[] map = new boolean[256]; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (map[val]) return false; map[val] = true; } return true; } } Complexity Analysis: Time Complexity: \(O(n)\) Space Complexity: \(O(n)\) Here is my Video Explanation