Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

karthikeya.bolla's soltions to chapter-1 of CTCI #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/DuplicateCharacters1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Version: 1.0
* Author: Karthikeya Bolla
*
* Cracking the coding interview - Question 1.1
*
* finding if there are duplicate occurrences of characters in string
*/

import java.io.Console;
import java.util.*;

public class DuplicateCharacters1
{
//brute force approach i.e. looking through all characters one by one without using another data structure
public void Method1()
{
int count = 0;
System.out.println("Enter the string");
Scanner scan = new Scanner(System.in);
String inputString = scan.next();
char[] charArr = inputString.toCharArray();
for(int i=0;i<charArr.length;i++)
{
for(int j=i+1;j<charArr.length;j++)
{
if(charArr[i] != charArr[j])
{
continue;
}
else
{
count++;
System.out.println("repetitions exist");
break;
}
}
}
if(count == 0)
{
System.out.println("repetitions don't exist");
}
}

//using HashSet or TreeSet data structure i.e. using another data structure
public void Method2()
{
HashSet<Character> hset = new HashSet<Character>();
Scanner scan = new Scanner(System.in);
System.out.println("enter the string");
String inputString = scan.next();
char[] charArr = inputString.toCharArray();
int count = 0;
for(int i=0;i<charArr.length;i++)
{
if(hset.add(charArr[i]))
{
continue;
}
else
{
count++;
System.out.println("repetitions exist");
break;
}
}
if(count == 0)
{
System.out.println("repetitions don't exist");
}
}

public static void main(String[] args)
{
DuplicateCharacters1 obj = new DuplicateCharacters1();
obj.Method1();
obj.Method2();
}
}
85 changes: 85 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/MatrixRotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Version: 1.0
* Author: Karthikeya Bolla
*
* Cracking the coding interview - Question 1.6
*
* NxN Matrix rotation by 90 degrees and 270 degrees
*/

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

public class MatrixRotation
{
//rotation by 90 degrees
public void Rotation()
{
System.out.println("Enter the value of N");
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int[][] originalMatrix = new int[N][N];
int[][] rotatedMatrix90 = new int[N][N];
int[][] rotatedMatrix270 = new int[N][N];

//generating some random matrix
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
originalMatrix[i][j] = 2*i+j;
}
}
//90 degrees rotated matrix
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
rotatedMatrix90[j][N - 1 - i] = originalMatrix[i][j];
}
}
//270 degrees rotated matrix (just out of interest)
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
rotatedMatrix270[N - 1 - j][i] = originalMatrix[i][j];
}
}
//displaying values
System.out.println("original matrix:");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(originalMatrix[i][j]+" ");
}
System.out.println();
}
System.out.println("90 degrees rotated matrix");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(rotatedMatrix90[i][j]+" ");
}
System.out.println();
}
System.out.println("270 degrees rotated matrix");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(rotatedMatrix270[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args)
{
MatrixRotation obj = new MatrixRotation();
obj.Rotation();
}
}


4 changes: 4 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/READ.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

The solutions to Chapter-1 are written by Karthikeya Bolla.

Solutions are written in Java
67 changes: 67 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/StringCompression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Version: 1.0
* Author: Karthikeya Bolla
*
* Cracking the coding interview - Question 1.5
*
* String compression
*/
import java.io.*;
import java.util.*;

public class StringCompression
{
public void Compress()
{
Scanner scan = new Scanner(System.in);
System.out.println("enter the string");
String originalStr = scan.nextLine();
//System.out.println(originalStr);
//System.out.println(originalStr.length());
int count = 0;
char prevChar = '\0';
char[] charArr = originalStr.toCharArray();
StringBuffer newString = new StringBuffer();

for(int i=0;i<charArr.length;i++)
{
if(i == 0)
{
prevChar = charArr[i];
count = 1;
}
else
{
if(charArr[i] == prevChar)
{
count++;
}
if(charArr[i] != prevChar)
{
newString.append(prevChar);
newString.append(count);
prevChar = charArr[i];
count = 1;
}
if(i == charArr.length-1)
{
newString.append(prevChar);
newString.append(count);
}
}
}
if(originalStr.length() < newString.length())
{
System.out.println(originalStr);
}
else
{
System.out.println(newString);
}
}
public static void main(String[] args)
{
StringCompression obj = new StringCompression();
obj.Compress();
}
}
83 changes: 83 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/StringPermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Version: 1.0
* Author: Karthikeya Bolla
*
* Cracking the coding interview - Question 1.3
*
* check for string permutation
*/

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

public class StringPermutation
{

public void CheckPermutation()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter string1:");
String str1 = scan.next();
System.out.println("Enter string2");
String str2 = scan.next();

char[] str1CharArr = str1.toCharArray();
char[] str2CharArr = str2.toCharArray();

//sorting str1CharArr
int lengthStr1 = str1CharArr.length;
char temp;
for(int i=0;i<lengthStr1;i++)
{
for(int j=i+1;j<lengthStr1;j++)
{
if(str1CharArr[i] > str1CharArr[j])
{
temp = str1CharArr[i];
str1CharArr[i] = str1CharArr[j];
str1CharArr[j] = temp;
}
}
}

//sorting str2CharArr
int lengthStr2 = str2CharArr.length;
for(int i=0;i<lengthStr2;i++)
{
for(int j=i+1;j<lengthStr2;j++)
{
if(str2CharArr[i] > str2CharArr[j])
{
temp = str2CharArr[i];
str2CharArr[i] = str2CharArr[j];
str2CharArr[j] = temp;
}
}
}

//comparing
int count = 0;
for(int i=0;i<str1CharArr.length;i++)
{
if(str1CharArr[i] == str2CharArr[i])
{
continue;
}
else
{
count++;
System.out.println("The strings are not permutation of each other");
break;
}
}
if(count == 0)
{
System.out.println("Strings are permutation of each other");
}
}
public static void main(String[] args)
{
StringPermutation obj = new StringPermutation();
obj.CheckPermutation();
}
}
31 changes: 31 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/Substring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Version: 1.0
* Author: Karthikeya Bolla
*
* Cracking the coding interview - Question 1.8
*
* checking if a string is part of actual string
*/
public class Substring
{
public void isSubstring(String str1, String str2)
{
String modifiedStr = "";
modifiedStr = modifiedStr + str1 + str1;
if(modifiedStr.contains(str2))
{
System.out.println("rotated string is contained in the original string");
}
else
{
System.out.println("rotated string is not contained in the original string");
}
}
public static void main(String[] args)
{
Substring obj = new Substring();
String originalStr = "waterbottle";
String rotatedStr = "erbottlewat";
obj.isSubstring(originalStr, rotatedStr);
}
}
42 changes: 42 additions & 0 deletions java/Karthikeya_Bolla_Chapter_1/Whitespaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Version: 1.0
* Author: Karthikeya Bolla
* Date: 12.29.2015
*
* Cracking the coding interview - Question 1.4
*
* Replace whitespaces with "%20"
*/

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

public class Whitespaces
{
public void editWhitespaces()
{
System.out.println("Enter string:");
Scanner scan = new Scanner(System.in);
String sentence = scan.nextLine();
String[] strArr = sentence.split(" ");
String finalString = "";

for(int i=0;i<strArr.length;i++)
{
if(i == 0)
{
finalString = strArr[i];
}
else
{
finalString = finalString +"%20"+strArr[i];
}
}
System.out.println(finalString);
}
public static void main(String[] args)
{
Whitespaces obj = new Whitespaces();
obj.editWhitespaces();
}
}
Loading