Data Structure and Dart Programming language problems

  • Home
  • Data Structure
    • Array and String
    • Linklist
  • Google Flutter
    • Google Flutter
    • DART
  • About Us
  • Home
  • Data Structure
    • Array and String
    • Linklist
  • Google Flutter
    • Google Flutter
    • DART
  • About Us

Wednesday, February 15, 2017

Arrays-String interview question 5

 Tech host     10:51 PM     array, array-interview-question, cracking-the-coding-interview, frequently-asked-question, interview-question, popular-string-question, string, string-interview-question, string-palindrome-question     No comments   

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 5
There are three types of edits that can be performed on strings: insert a character, replace a character or remove a character; Given two strings, write a function to check if they are one edit (or zero edits) away. EXAMPLE
pale, ple -> true
pale. bake -> false
pale. bale -> true
pales. pale -> true

Here i am providing two solution for this problem one in which i have created separate functions for edit(insert , remove) and replace function, Here one thing is important to see that insert and remove are two side of one coin means to say that insert means that we are just adding one character to the previous sting and remove means that we are just deleting one char from the previous string so we can use only one function by just checking the length of two strings for insert and remove.

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.

Solution

You can find the solution for this problem here.
Solution 1 using  java separate functions.

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
 public static void main (String[] args) throws java.lang.Exception
 {
  System.out.println(oneInsert("apple","apples"));
 }

  public static boolean oneEdit(String a, String b)
 {
  boolean ctr=false;
    for(int i=0;i<a.length();i++){
   if(a.charAt(i)!=b.charAt(i)){
    if(ctr)
     return false;
    ctr=true; 
   }
  }
  return true;
 }
 public static boolean oneInsert(String s1,String s2)
 {
  String a = s1.length()>s2.length()?s1:s2;
  String b = s1.length()>s2.length()?s2:s1;
  for(int i=0,j=0;i<a.length()&&j<b.length();)
  {
   if(a.charAt(i)!=b.charAt(j))
   {
    if(i!=j)
    {
     return false;
    }
    i++; 
   }
   else
   {
    i++;
    j++;
   }
  }
  return true;
 }
}
Solution 2 using  java combined functions.

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
 public static void main (String[] args) throws java.lang.Exception
 {
  System.out.println(oneEdit("banana","banan"));
 }
 public static boolean oneEdit(String s1,String s2)
 {
  String a = s1.length()>s2.length()?s1:s2;
  String b = s1.length()>s2.length()?s2:s1;
  boolean ctr=false;

  for(int i=0,j=0;i<a.length()&&j<b.length();)
  {
   if(a.charAt(i)!=b.charAt(j))
   {
    if(i!=j)
    {
     return false;
    }
    if(ctr)
    {
     return false;
    }
    ctr=true;
    i++;
   }
   i++;
   j++;
  }
 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.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Blog Archive

  • ▼  2017 (18)
    • ►  August (2)
    • ►  March (10)
    • ▼  February (6)
      • Arrays-String interview question 5
      • Arrays-String interview question 4
      • Arrays-String interview question 3
      • Arrays-String interview question 2
      • Arrays-String interview question 1
      • Arrays-String interview question solution
  • ►  2016 (1)
    • ►  December (1)
  • ►  2013 (1)
    • ►  August (1)

Popular Posts

  • Dart replacing the node in the abstract syntax tree
    Dart replacing the node in the abstract syntax tree Here is the solution for that. import 'package:analyzer/analyzer.dart'...
  • How to print the node of the given linked list
    Linklist Interview Question How to print the node of the given linked list If you're new to linked lists, this is a great ex...
  • Array Left Rotation
    Array Interview Question Hacker Rank Question Array Left Rotation Hey guys i am providing the solutions for the question given in...
  • Arrays-String interview question 4
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Ch...
  • Reverse of a given linked list
    linked list Interview Question Reverse of a given linked list You’re given the pointer to the head node of a linked list. Change...
  • Insert Node at nth position in a linked list
    linked list Interview Question Insert Node at nth position in a linked list You’re given the pointer to the head node of a linke...
  • Arrays-String interview question 5
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the C...
  • Arrays-String interview question 3
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Ch...
  • Arrays-String interview question 7
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Cha...
  • Arrays-String interview question 1
    Cracking the coding interview Arrays and String question solutions Hey guys i am providing the solutions for the question given i...

Categories

  • about-scra
  • abstract-syntax-tree
  • array
  • array-interview-question
  • array-left-rotation
  • compare-two-linked-list
  • cracking-the-coding-interview
  • dart
  • dart-analyzer
  • flutter
  • frequently-asked-question
  • google-flutter
  • interview-question
  • java
  • left-rotation
  • linklist-interview-question
  • linklist-print-a-node
  • linklist-question
  • popular-linklist-question
  • popular-string-question
  • scra
  • scra-2018
  • string
  • string-interview-question
  • string-palindrome-question

About Me

Tech host
I am a blogger interested in programming and coding challenges. I am interested in knowing about the new technologies and new tech products that are launched daily and to write about them.
View my complete profile

Label

  • about-scra
  • abstract-syntax-tree
  • array
  • array-interview-question
  • array-left-rotation
  • compare-two-linked-list
  • cracking-the-coding-interview
  • dart
  • dart-analyzer
  • flutter
  • frequently-asked-question
  • google-flutter
  • interview-question
  • java
  • left-rotation
  • linklist-interview-question
  • linklist-print-a-node
  • linklist-question
  • popular-linklist-question
  • popular-string-question
  • scra
  • scra-2018
  • string
  • string-interview-question
  • string-palindrome-question
Powered by Blogger.
Revoltify

Revoltify

Copyright © Data Structure and Dart Programming language problems | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates