Hacker Rank

Coding Challenge Coding Live

//if second string DOES contain character from first string

//iterate over every character in first string

// essentially (number of uncommon characters = total characters – common characters)

public class Solution public static int numberNeeded(String first, String second) // essentially (number of uncommon characters = total characters – common characters) //Create a copy of the first string to manipulate, firstCopy – to manipulate, first – to iterate String firstCopy = first; //iterate over every character in first string for(char c : first.toCharArray()) //if second string contains the character it will return a 0 or positive value depending on position, else -1 dexOf(c) != -1) //if second string DOES contain character from first string //delete the first found character match from firstCopy firstCopy = firstCopy.replaceFirst(c + , ); //delete the first found character match from the second string second = second.replaceFirst(c + , ); //After this for() loop what we have left are characters in both strings not found in each other. //So we would have to delete this many characters to get the anagram. // firstCopy.length() + second.length are the number of deletions needed. return firstCopy.length() + second.length(); public static void main(String[] args) Scanner in = new Scanner(System.in); String a = in.next(); String b = in.next(); System.out.println(numberNeeded(a, b));

//Were going to start with a[1] and copy a[j-1] to a[j]

//delete the first found character match from firstCopy

//Now copy temp value to the right most element in a[]. ie a[n-1]

//Create a copy of the first string to manipulate, firstCopy – to manipulate, first – to iterate

//if second string contains the character it will return a 0 or positive value depending on position, else -1

public class Solution public static void main(String[] args) Scanner in = new Scanner(System.in); //INPUTS //n = size of array int n = in.nextInt(); //k = number of left rotations int k = in.nextInt(); int a[] = new int[n]; //each element put into array a[] for(int a_i=0; a_i n; a_i++) a[a_i] = in.nextInt(); //CALCULATION //for every left shift… for(int i = 0; i k; i++) //Were going to copy the left most value in a[] to a temp variable int temp = a[0]; //Were going to start with a[1] and copy a[j-1] to a[j] for(int j=1; j n; j++) a[j-1] = a[j]; //Now copy temp value to the right most element in a[]. ie a[n-1] a[n-1] = temp; //DISPLAY LOOP for(int i = 0; i n; i++) System.out.print(a[i]+ );

//After this for() loop what we have left are characters in both strings not found in each other.

Link to Question:Arrays: Left Rotation

//So we would have to delete this many characters to get the anagram.

//Were going to copy the left most value in a[] to a temp variable

// firstCopy.length() + second.length are the number of deletions needed.

//delete the first found character match from the second string

Leave a Reply