Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSC-220 HW2 Isomorphic String Determination Program
/*
* Name: Prof. Antonio C. Silvestri
* Date: 9/22/2018
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW2
* Email: silvestri@stcc.edu
* Determine if two strings are isomorphic
*/
import java.util.Scanner;
import silvestri.LinearSearch;
public class IsomorphicStrings {
private static boolean isIsomorphic(String x, String y) {
if (x.length() != y.length())
return false;
char[] xchars = x.toCharArray();
char[] ychars = y.toCharArray();
char[] reviewedChars = new char[xchars.length];
char[] replacementChars = new char[xchars.length];
int reviewedCharsCount = 0;
for (int i = 0; i < xchars.length; i++) {
int index = LinearSearch.linearSearch(reviewedChars, reviewedCharsCount, xchars[i]);
if (index == -1) {
reviewedChars[reviewedCharsCount] = xchars[i];
replacementChars[reviewedCharsCount] = ychars[i];
reviewedCharsCount++;
}
else if (replacementChars[index] != ychars[i])
return false;
}
return true;
}
//**********************************************
private static void process(Scanner sc, String args[]) {
System.out.print("Enter String 1: ");
String x = sc.nextLine();
System.out.print("Enter String 2: ");
String y = sc.nextLine();
System.out.printf("%s and %s are %sisomorphic.\n", x, y, isIsomorphic(x, y) ? "" : "not ");
}
//**********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
//**********************************************
public static void main(String args[]) {
final String TITLE = "IsoMorphic String Determinator V1";
final String CONTINUE_PROMPT = "Do this again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment