Guest User

Untitled

a guest
Jan 21st, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. public class Anagram1{
  2. public static boolean check(char[] c1,char[] c2,int l1,int l2){
  3.  
  4. /*if the lengths of the strings are not equal,they cannot be anagrams of each other */
  5.  
  6. if(l1!=l2){
  7. return false;
  8. }
  9.  
  10. int count = 0;
  11.  
  12. //nested for loops to compare the characters of both strings.
  13. //if the characters match,then the strings are anagrams of each other.
  14. for(int i=0; i<l1; i++){
  15. for(int j=0; j<l2; j++){
  16. if(c1[i]==c2[j]){
  17. count++;
  18. break; //"break" used so as to count only a single occurrence of a
  19. } //character.
  20. }
  21. }
  22. if(count==l1 && count==l2){
  23. return true;
  24. }
  25. return false;
  26. }
  27.  
  28. public static void main(String[] args) {
  29.  
  30. String s1 = "abcd";
  31.  
  32. String s2 = "acdb";
  33.  
  34. char[] c1 = s1.toCharArray();
  35.  
  36. char[] c2 = s2.toCharArray();
  37.  
  38. int l1 = s1.length();
  39.  
  40. int l2 = s2.length();
  41.  
  42. System.out.println(check(c1,c2,l1,l2));
  43. }
  44. }
  45.  
  46. String s1 = "abbd";
  47. String s2 = "acdb";
  48.  
  49. import java.util.ArrayList;
  50. import java.util.Collections;
  51.  
  52. public class Anagram1 {
  53. public static void main(String[] args) {
  54. String s1 = "abcd";
  55. String s2 = "adbc";
  56. System.out.println(check(s1, s2));
  57. }
  58.  
  59. public static boolean check(String s1, String s2) {
  60. // Remove white space and change to lower case.
  61. s1 = s1.replace(" ","").toLowerCase();
  62. s2 = s2.replace(" ","").toLowerCase();
  63.  
  64. // If they are equal, return true. If they are not the same length,
  65. // return false.
  66. if (s1.equals(s2)) {
  67. return true;
  68. } else if (s1.length() != s2.length()) {
  69. return false;
  70. }
  71.  
  72. // Convert to ArrayList for sorting.
  73. ArrayList<Character> sl1 = new ArrayList<Character>();
  74. ArrayList<Character> sl2 = new ArrayList<Character>();
  75.  
  76. for (char ch1 : s1.toCharArray()) {
  77. sl1.add(ch1);
  78. }
  79.  
  80. for (char ch2 : s2.toCharArray()) {
  81. sl2.add(ch2);
  82. }
  83.  
  84. // Sort both ArrayLists
  85. Collections.sort(sl1);
  86. Collections.sort(sl2);
  87.  
  88. // If they are equal return true, else return false.
  89. if (sl1.equals(sl2)) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. }
  96.  
  97. if(count==l1 && count==l2){
  98. return true;
  99. }
  100.  
  101. public static void main(String[]args){
  102. //Diff lengths = false
  103. System.out.println(check("acd", "bacd"));
  104. //Same letters, mixed = true
  105. System.out.println(check("abcd", "bacd"));
  106. //Same string = true
  107. System.out.println(check("abcd", "abcd"));
  108. //Diff letters = false
  109. System.out.println(check("abcd", "gacd"));
  110. //Same characters = true
  111. System.out.println(check("abcd123$", "1b2a3c$d"));
  112. //Multiple occurances of a letter = true
  113. System.out.println(check("aaacddbg", "acadadgb"));
  114. }
  115.  
  116. public static boolean check(String c1, String c2){
  117. if(c1.length() != c2.length()){
  118. return false;
  119. }
  120.  
  121. char[] word1 = c1.toCharArray();
  122. char[] word2 = c2.toCharArray();
  123.  
  124. Map<Character, Integer> map1 = new HashMap<>();
  125. Map<Character, Integer> map2 = new HashMap<>();
  126.  
  127. for(int i = 0; i < word1.length; i++){
  128. map1.put(word1[i], (map1.get(word1[i]) == null) ? 1 : map1.get(word1[i])+1);
  129. }
  130.  
  131. for(int i = 0; i < word2.length; i++){
  132. map2.put(word2[i], (map2.get(word2[i]) == null) ? 1 : map2.get(word2[i])+1);
  133. }
  134.  
  135. if(map1.equals(map2)){
  136. return true;
  137. }else{
  138. return false;
  139. }
  140. }
  141.  
  142. public class Anagram{
  143. public static boolean check(char[] c1,char[] c2,int l1,int l2){
  144.  
  145. /*If the lengths are unequal,return false.*/
  146. if(l1!=l2){
  147. return false;
  148. }
  149.  
  150. int[] count1 = new int[256];
  151. int[] count2 = new int[256];
  152.  
  153. for(int i=0; i<l1; i++){
  154. count1[c1[i]]++;
  155. }
  156.  
  157. for(int j=0; j<l2; j++){
  158. count2[c2[j]]++;
  159. }
  160.  
  161. /*Comparing both the count arrays.*/
  162. for(int k=0; k<count1.length; k++){
  163. if(count1[k]!=count2[k]){
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169. public static void main(String[] args) {
  170.  
  171. String s1 = "anagram";
  172. String s2 = "nagarnm";
  173. char[] ch1 = s1.toCharArray();
  174. char[] ch2 = s2.toCharArray();
  175. int len1 = s1.length();
  176. int len2 = s2.length();
  177.  
  178. System.out.println(check(ch1,ch2,len1,len2));
  179. }
  180. }
Add Comment
Please, Sign In to add comment