fork(1) download
  1. import java.util.*;
  2.  
  3. /**
  4.  * Author:- nikhil.lohia
  5.  */
  6. class Ideone {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. //1-Non Repeated Characters in String : return the unique characters in a given letter.
  11. ArrayList<String> uniqueCharsResults = uniqueLetters("studyalgorithms");
  12. for (int i = 0; i < uniqueCharsResults.size(); i++) {
  13. System.out.print(uniqueCharsResults.get(i) + " ");
  14. }
  15.  
  16. // Code obtained from http://w...content-available-to-author-only...s.com
  17. // Feel free to copy but please acknowledge wherever possible
  18.  
  19. //2- Anagram Strings: given two strings, check if they’re anagrams or not.
  20. //Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and capitalization.
  21. //Each letter should have the same count in both strings.
  22. //For example, ‘Eleven plus two’ and ‘Twelve plus one’ are meaningful anagrams of each other.
  23. //complexity is logn
  24. boolean result = isAnagram("Eleven plus two", "Twelve plus one");
  25. System.out.println("Anagram Test using sorting, Result is: " + result);
  26.  
  27. //3- Find Word Positions in Text : Given a text file
  28. //and a word, find the positions that the word
  29. //occurs in the file. We’ll be asked to find the positions of
  30. //many words in the same file.
  31. findWordPosition("have a nice try did you try try try", "try");
  32.  
  33. //4-Remove Duplicate Characters in String: Remove duplicate characters
  34. //in a given string keeping only the first occurrences. For example,
  35. //if the input is ‘tree traversal’ the output will be ‘tre avsl’.
  36. Set<Character> set = removeDuplicateChars("tree traversal");
  37. for (Character entry : set) {
  38. System.out.println(entry);
  39. }
  40.  
  41. }
  42.  
  43. public static ArrayList<String> uniqueLetters(String text) {
  44. //create a hashmap with key and value pairs, where key is the each letter and
  45. //value is the count of each letter of a given text
  46. Map<Character, Integer> map = new HashMap<Character, Integer>();
  47. //to store unique letters create an arraylist
  48. ArrayList<String> uniqueChars = new ArrayList<String>();
  49. //we check each letter in a given text, so in this problem we don't need StringTokenizer
  50. for (int i = 0; i < text.length(); ++i) {
  51. if (map.containsKey(text.charAt(i))) {
  52. int count = map.get(text.charAt(i));
  53. map.put(text.charAt(i), count + 1);
  54. } else {
  55. map.put(text.charAt(i), 1);
  56. }
  57. }
  58. // Loop in a hashSet to get keys and values
  59. Iterator iterator = map.entrySet().iterator();
  60. while (iterator.hasNext()) {
  61. Map.Entry pairs = (Map.Entry) iterator.next();
  62. System.out.println(pairs.getKey() + " = appeared " + pairs.getValue() + " times");
  63. if (pairs.getValue().equals(1)) {
  64. uniqueChars.add(pairs.getKey().toString());
  65. }
  66. iterator.remove(); // avoids a ConcurrentModificationException
  67. }
  68. return uniqueChars;
  69. }
  70.  
  71. public static boolean isAnagram(String text1, String text2) {
  72. //1st method is to sort both strings and check one by one
  73. //sorting adds an complexity of nlogn (e.g. merge sort)
  74. //this code considers space, punctuation etc.
  75. //getChars method may also be used here
  76. System.out.println("");
  77. if (text1.length() != text2.length()) {
  78. System.out.println("inside isAnagram: " + text1.length() + " " + text2.length());
  79. return false;
  80. } else {
  81. String sorted1 = convertToCharArrayFromString(1, text1);// 1 is for sorted return
  82. String sorted2 = convertToCharArrayFromString(1, text2);
  83. for (int i = 0; i < sorted1.length(); i++) {
  84. if (sorted1.charAt(i) != sorted2.charAt(i)) {
  85. return false;
  86. }
  87. }
  88. }
  89. return true;
  90. }
  91.  
  92. public static String convertToCharArrayFromString(int option, String text) {
  93. char[] content1 = text.toLowerCase().toCharArray();
  94. if (option == 1) {
  95. Arrays.sort(content1);
  96. String sorted1 = new String(content1);
  97. return sorted1;
  98. } else {
  99. String unsorted1 = new String(content1);
  100. return unsorted1;
  101. }
  102. }
  103.  
  104. public static void findWordPosition(String text, String word) {
  105. Map<String, ArrayList<Integer>> map = createInvertedIndex(text);
  106. // for (Map.Entry<String, ArrayList<Integer>> entry : map.entrySet()) {
  107. ArrayList<Integer> position = map.get(word);
  108. // System.out.println("Key " + entry.getKey() + entry.getValue());
  109. for (Integer value : position) {
  110. System.out.println("Key: " + word + " its positions " + value);
  111. }
  112. }
  113.  
  114. public static Map<String, ArrayList<Integer>> createInvertedIndex(String text) {
  115. //assume that text is given with appropriate
  116. //letters so no need for regular expressions
  117. //we can create a map with a key of String (each word) and
  118. //their positions that is integer array
  119. Map<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
  120. String[] words = text.split(" ");
  121. for (int i = 0; i < words.length; i++) {
  122. if (map.containsKey(words[i])) {
  123. ArrayList<Integer> position = map.get(words[i]);
  124. position.add(i);
  125. map.put(words[i], position);
  126. } else {
  127. ArrayList<Integer> position = new ArrayList<Integer>();
  128. position.add(i);
  129. map.put(words[i], position);
  130. }
  131. }
  132. return map;
  133. }
  134.  
  135. public static Set<Character> removeDuplicateChars(String text) {
  136. // for insertion sort order LinkedHashSet is used
  137. Set<Character> set = new LinkedHashSet<Character>();
  138. for (int i = 0; i < text.length(); i++) {
  139. if (!set.contains(text.charAt(i))) {
  140. set.add(text.charAt(i));
  141. }
  142. }
  143. return set;
  144. }
  145.  
  146. }
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
a = appeared 1 times
d = appeared 1 times
g = appeared 1 times
h = appeared 1 times
i = appeared 1 times
l = appeared 1 times
m = appeared 1 times
o = appeared 1 times
r = appeared 1 times
s = appeared 2 times
t = appeared 2 times
u = appeared 1 times
y = appeared 1 times
a d g h i l m o r u y 
Anagram Test using sorting, Result is: true
Key: try its positions 3
Key: try its positions 6
Key: try its positions 7
Key: try its positions 8
t
r
e
 
a
v
s
l