Task - String Programs - Anagrams #34
Replies: 43 comments 1 reply
-
|
import java.util.Scanner; public class Anagram { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package StringOperations; public class Anagram { public static void main(String[] args) { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package Week4; import java.util.Scanner; public class Anagram { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Arrays; public class Anagram { }` |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Arrays; } |
Beta Was this translation helpful? Give feedback.
-
|
He Who Must Not Be Named `import java.util.Arrays; class Main { } |
Beta Was this translation helpful? Give feedback.
-
package week4;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AnagramStrings {
public static void main(String[] args) {
String str1 = "Tom Marvolo Riddle";
String str2 = "I am Lord Voldemort";
str1 = str1.replaceAll("\\s", "");
str2 = str2.replaceAll("\\s", "");
int count = 0;
for (char c : str1.toCharArray()) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
count++;
}
for (char c : str2.toCharArray()) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
count--;
}
if(count == 0) {
System.out.println("they are anagrams.");
Set<Character> s= new HashSet<>();
for (char c : (str1 + str2).toCharArray()) {
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')))
s.add(c);
}
System.out.println("Unique characters are : " + s.size() + " They are:"+s);
}
else
System.out.println("they are not anagrams.");
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.io.; class anagram { } |
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.*; } |
Beta Was this translation helpful? Give feedback.
-
|
public class MyClass { } |
Beta Was this translation helpful? Give feedback.
-
`package Strings; import java.util.*; public class Anagrams { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package com.src;
import java.util.Arrays;
public class StringAnagram {
public static void main (String [] args) {
String str1="Tom Marvolo Riddle";
String str2="I am Lord Voldemort";
//Converting both the string to lower case and removing the white spaces.
str1 = str1.toLowerCase().replaceAll("\\s", "");
str2 = str2.toLowerCase().replaceAll("\\s", "");;
//Checking for the length of strings
if (str1.length() != str2.length()) {
System.out.println("Both the strings are not anagram");
}
else {
//Converting both the arrays to character array
char[] string1 = str1.toCharArray();
char[] string2 = str2.toCharArray();
//Sorting the arrays using in-built function sort ()
Arrays.sort(string1);
Arrays.sort(string2);
//Comparing both the arrays using in-built function equals ()
if(Arrays.equals(string1, string2) == true) {
System.out.println("Both the strings are anagram");
}
else {
System.out.println("Both the strings are not anagram");
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
AnagramDemo.javaimport java.util.Scanner;
public class AnagramDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1,s2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first String:");
s1 = sc.nextLine();
System.out.println("Enter second String:");
s2 = sc.nextLine();
s1 = s1.toLowerCase().replace(" ", "");
s2 = s2.toLowerCase().replace(" ", "");
char buff[] = s1.toCharArray();
for(char ch: buff) {
if(s2.contains(Character.toString(ch))) {
s2 = s2.replaceFirst(Character.toString(ch), "");
}
}
if(s2.isEmpty()) {
System.out.println("The Strings are anagrams.");
}
else {
System.out.println("The Strings are not anagrams.");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
package mypackage;
import java.util.Arrays;
public class Anagrams {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Race";
String s2 = "Care";
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
if(s1.length() == s2.length()) {
char[] charArray1 = s1.toCharArray();
char[] charArray2 = s2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
boolean result = Arrays.equals(charArray1, charArray2);
if(result) {
System.out.println(s1 + " and " + s2 + " are anagram.");
}else {
System.out.println(s2 + " and " + s2 + " are not anagram.");
}
}else {
System.out.println(s1 + " are not anagram.");
}
}
}
```java |
Beta Was this translation helpful? Give feedback.
-
package com.spring.calculator;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class Anagrams {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter first String : ");
String str1 = scan.nextLine();
System.out.println("Enter Second String : ");
String str2 = scan.nextLine();
str1 = str1.replace(" ", "").toLowerCase();
str2 = str2.replace(" ", "").toLowerCase();
System.out.println(str2);
if(str1.length()!=str2.length()) {
System.out.println("Not an Anagram");
}
else {
char[] char1 = str1.toCharArray();
char[] char2 = str2.toCharArray();
Arrays.sort(char1);
Arrays.sort(char2);
if(Arrays.equals(char1,char2)) {
System.out.println("It's Anagram");
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < str1.length(); i++) {
map.put(char1[i], i);
}
System.out.println("unique character count : "+map.size());
}
else
System.out.println("Not an Anagram");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
package com.anagram; import java.util.Arrays; public class AnagramString { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
https://github.com/harsha916/Anagram |
Beta Was this translation helpful? Give feedback.
-
package com.string.excercise;
import java.util.Scanner;
public class MainAppAnagram {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str1 = "";
String str2 = "";
boolean anagram = true;
System.out.print("Enter String 1 = ");
str1 = sc.nextLine();
System.out.print("Enter String 2 = ");
str2 = sc.nextLine();
str1 = str1.toLowerCase().replaceAll(" ", "");
str2 = str2.toLowerCase().replaceAll(" ", "");
for(int i=0; i<str1.length(); i++){
String temp = str1.charAt(i) + "";
if(!str2.contains(temp)){
anagram = false;
}
}
for(int i=0; i<str2.length(); i++){
String temp = str2.charAt(i) + "";
if(!str1.contains(temp)){
anagram = false;
}
}
if(anagram){
System.out.println("Both the string are anagram of each other");
}else{
System.out.println("Both the string are not anagram of each other");
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
package src.string.anagram.program;
import java.util.Arrays;
public class StringAnagrm {
public static boolean isAnagram(String str1, String str2) {
str1=str1.replaceAll("\\s", "");
str2=str2.replaceAll(" ", "");
if(str1.length()!=str2.length()) {
return false;
}else {
char[] c1 = str1.toLowerCase().toCharArray();
char[] c2= str2.toLowerCase().toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//using uquals and sort method
//using builder and hashpam
String str1="Tom Marvolo Riddle";
String str2="I am Lord Voldemort";
System.out.println(isAnagram("keep","peeks"));
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
He Who Must Not Be Named
Two strings are called anagrams if they contain the same set of characters but in a different order.
Example: keep, peek
Write a Java program to check whether two strings are anagram or not.
The program takes the following two strings as inputs and finds out whether they are anagrams of each other.
If the strings are anagrams of each other, print the count of unique characters in either of them.
Beta Was this translation helpful? Give feedback.
All reactions