Posts

Showing posts from August, 2019

Implement strStr() or indexOf() Java Solution - The Coding Shala

Home >> Interview Questions >> Implement strStr() Implement strStr() or indexOf() Java Solution Problem: Return the index of the first occurrence of needle in the haystack, or -1 if the needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle = "bba" Output: -1 Implement strStr() or indexOf() Java Solution Approach: If the needle char does not match with haystack return -1 else continue checking. Java  class Solution { public int strStr ( String haystack , String needle ) { int len1 = haystack . length (); int len2 = needle . length (); if ( len2 == 0 ) return 0 ; int index = - 1 ; if ( len2 > len1 ) return - 1 ; for ( int i = 0 ; i < len1 ; i ++){ int flag = 0 ; if (...

Longest Common Prefix Java Solution - The Coding Shala

Home >> Interview Questions >> Longest Common Prefix Longest Common Prefix Problem: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Note: All given inputs are in lowercase letters a-z. Longest Common Prefix Java Solution Approach: We will check character at every index of every string given in array if not matched then will break the loop. Java Code::  class Solution { public String longestCommonPrefix ( String [] strs ) { int len = strs . length ; if ( strs == null || len == 0 ) return "" ;...

Introduction to String - The Coding Shala

Home >> Data Structures >> Introduction to String Introduction to String A String is a collection of Unicode characters and similar to the arrays. We can perform almost all the operations on the string that we used in an array. Below is some function that we use frequently with Strings. Compare Function The String has its own compare function, unlike the == operator. We can compare two strings in the following ways: we can use == operator, like string1 == string2. we can use equals() method. like string1.equals(string).  we can use compareTo() method. For example string1.compareTo(string2). Note: The equals() method compares the value inside string while == operator compares the value of two object references to check if they refer to the same String instance or not. Both equals() and == returns true and false. The Java String compareTo() method is used for comparing two string lexicographically. If both the strings are the same the...

Add Binary Java Solution - The Coding Shala

Home >> Interview Questions >> Add Binary Add Binary Problem: Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contain only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101" Add Binary Java Solution Approach: Traverse from backward both string and check carry. Java:  class Solution { public String addBinary ( String a , String b ) { String ans = "" ; int len1 = a . length ()- 1 ; int len2 = b . length ()- 1 ; if ( len1 < 0 && len2 < 0 ) return "0" ; int carry = 0 ; while ( len1 >= 0 || len2 >= 0 ){ int tmp = carry ; if ( len1 >= 0 ) tmp += a . charAt ( len1 )...

Design Linked List - The Coding Shala

Home >> Data Structure >> Design Linked List Design Linked List Problem: Design your implementation of the linked list. You can choose to use them a singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: Val and next. Val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed. Implement these functions in your linked list class: get(index): Get the value of the index-th node in the linked list. If the index is invalid, return -1. addAtHead(val): Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. addAtTail(val): Append a node of value val to the last element of the linked list....