diff --git a/java/Karthikeya_Bolla_Chapter_1/DuplicateCharacters1.java b/java/Karthikeya_Bolla_Chapter_1/DuplicateCharacters1.java new file mode 100644 index 00000000..435f0150 --- /dev/null +++ b/java/Karthikeya_Bolla_Chapter_1/DuplicateCharacters1.java @@ -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 hset = new HashSet(); + 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 str1CharArr[j]) + { + temp = str1CharArr[i]; + str1CharArr[i] = str1CharArr[j]; + str1CharArr[j] = temp; + } + } + } + + //sorting str2CharArr + int lengthStr2 = str2CharArr.length; + for(int i=0;i str2CharArr[j]) + { + temp = str2CharArr[i]; + str2CharArr[i] = str2CharArr[j]; + str2CharArr[j] = temp; + } + } + } + + //comparing + int count = 0; + for(int i=0;i