Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Dec 7th, 2018 63 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. package StringLabs;
  2.  
  3. public class Anagram {
  4.  
  5.     public static void main(String[]args){
  6.         String s1, s2;
  7.         s1="hello";
  8.         s2="olelh";
  9.  
  10.         if(isAnagram(s1,s2)){
  11.             System.out.println("Yup");
  12.         }
  13.         else{
  14.             System.out.println("Nope");
  15.         }
  16.  
  17.  
  18.     }
  19.  
  20.     public static boolean isAnagram(String s1, String s2){
  21.         int i=0, j=0;
  22.         boolean isAnagram=true;
  23.         //s1 search loop!
  24.         if(s1.length()!=s2.length()){
  25.             isAnagram=false;
  26.         }
  27.  
  28.         while(i<s1.length()||isAnagram){
  29.             boolean letterFound=false;
  30.             while(j<s2.length()||!letterFound){//s2 search loop
  31.                 //check to see is s1 char and s2 char are equal
  32.                 if(s1.substring(i,i+1).equals(s2.substring(j,j+1))){
  33.                     letterFound=true;
  34.                     removeChar(s2, j);
  35.                 }
  36.  
  37.                 j++;
  38.             }//end s2 search
  39.             if(!letterFound){
  40.                 isAnagram=false;
  41.             }
  42.             j=0;
  43.             i++;
  44.         }
  45.  
  46.         return isAnagram;
  47.     }
  48.  
  49.     public static String removeChar(String s, int i){
  50.         return s.substring(0, i)+s.substring(i+1);
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top