Skip to content

Instantly share code, notes, and snippets.

@PatheticMustan
Last active September 25, 2023 04:38
Show Gist options
  • Save PatheticMustan/5fb3d85556b29b7ee4f3ee6b291df127 to your computer and use it in GitHub Desktop.
Save PatheticMustan/5fb3d85556b29b7ee4f3ee6b291df127 to your computer and use it in GitHub Desktop.
import java.lang.StringIndexOutOfBoundsException;
import java.util.Scanner;
public class Palindrome {
// Part 1
// O(n)
public static boolean palindromeIterative(String input) {
// filter text so it only contains lowercase letters and numbers
input = input.toLowerCase().replaceAll("[^a-z0-9]", "");
// pairs the first half to the second half, so we only
// need to iterate n/2 times
// personally I like this method more
int n = input.length();
for (int i=0; i<n/2; i++) {
if (input.charAt(i) != input.charAt(n-i-1)) return false;
}
return true;
}
// O(n)
public static boolean palindromeRecursive(String input) {
input = input.toLowerCase().replaceAll("[^a-z0-9]", "");
// this is kind of self explanatory, checks if the two edges match,
// and recurses all the stuff in the middle
int n = input.length();
if (n <= 1) return true;
if (input.charAt(0) != input.charAt(n-1)) return false;
return palindromeRecursive(input.substring(1, n-1));
}
// Part 2
public static boolean anagramChecker(String x, String y) {
x = x.toLowerCase().replaceAll("[^a-z0-9]", "");
y = y.toLowerCase().replaceAll("[^a-z0-9]", "");
/**
* personally I would just break the strings into char arrays,
* sort, then compare... I don't think that's what the professor
* intended for this assignment though, so I'm just going to
* count how many times each letter appears in the first one, then
* subtract with the letters in the last one.
* we'll know they're anagrams if it results in an all zero'd out array
**/
int count[] = new int[36]; // 26 + 10
// we count the instances of each letter
for (int i=0; i<x.length(); i++) {
// if it's a letter... otherwise it's a number
int index = -1;
if (x.charAt(i) >= 'a') index = x.charAt(i)-'a';
else index = x.charAt(i)-'0'+26;
count[index]++;
}
// now we decrement >:3
for (int i=0; i<y.length(); i++) {
int index = -1;
if (y.charAt(i) >= 'a') index = y.charAt(i)-'a';
else index = y.charAt(i)-'0'+26;
count[index]--;
}
// if it's nonzero at any point, we've either undercounted or overcounted a letter
for (int i=0; i<36; i++) if (count[i] != 0) return false;
return true;
}
public static String addSubstring(String input, String substring, int index) {
/**
* i asked the discord if we need to handle errors...
* i don't know why they didn't put anything about error handling in the
* assignment sheet, but i'm not about to get points off for something
* silly like missing this
*
* substring the part before, add the letter, then substring the part after
**/
if (index > input.length()) throw new StringIndexOutOfBoundsException("Index is too big for this string");
return input.substring(0, index) + substring + input.substring(index, input.length());
}
public static int getLength(String input) {
// cmon.
return input.length();
}
public static int occurrenceCounter(String input, String substring) {
/**
* consider that "aaa" contains two occurances of "aa"
* occurances may overlap, so we only do (index+1) when
* we use indexOf instead of (index+substring.length())
**/
int index = -1;
int occurances = 0;
do {
index = input.indexOf(substring, index+1);
if (index != -1) occurances++;
} while (index != -1);
return occurances;
}
public static String sentenceReversal(String input) {
// in Javascript this would just be input.split(" ").reverse().join(" ")
// i don't think I'm allowed to use ArrayLists, so whatever
char lastLetter = input.charAt(input.length()-1);
if (lastLetter == '.' || lastLetter == '?') {
// remove last letter from string, we'll add the end punctuation later
input = input.substring(0, input.length()-1);
} else {
// otherwise set to null char
lastLetter = 0;
}
String[] words = input.split(" ");
String result = "";
for (int i=0; i<words.length; i++) {
result += words[words.length-i-1];
if (i < words.length-1) result += " ";
}
if (lastLetter != 0) result += lastLetter;
return result;
}
// Additional Requirements
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the App\n\t1. Palindrome Check\n\t2. Anagram Check\n\t3. Add Substring\n\t4. Get Length\n\t5. Count Occurances\n\t6. Reverse Sentence\n\t7. Quit");
int option = -1;
while (option != 7) {
System.out.print("Choose an option: ");
option = sc.nextInt();
System.out.println("");
switch (option) {
case 1: {
System.out.println("String?");
// after consuming an int, you need to consume an empty line to take in a new line
sc.nextLine();
String a = sc.nextLine();
System.out.println("\nOutput: " + palindromeIterative(a));
break;
}
case 2: {
System.out.println("String?");
sc.nextLine();
String a = sc.nextLine();
System.out.println("String?");
String b = sc.nextLine();
System.out.println("\nOutput: " + anagramChecker(a, b));
break;
}
case 3: {
System.out.println("String?");
sc.nextLine();
String a = sc.nextLine();
System.out.println("Substring?");
String b = sc.nextLine();
int c = sc.nextInt();
System.out.println("\nOutput: " + addSubstring(a, b, c));
break;
}
case 4: {
System.out.println("String?");
sc.nextLine();
String a = sc.nextLine();
System.out.println("\nOutput: " + getLength(a));
break;
}
case 5: {
System.out.println("String?");
sc.nextLine();
String a = sc.nextLine();
System.out.println("Substring?");
String b = sc.nextLine();
System.out.println("\nOutput: " + occurrenceCounter(a, b));
break;
}
case 6: {
System.out.println("String?");
sc.nextLine();
String a = sc.nextLine();
System.out.println("\nOutput: " + sentenceReversal(a));
break;
}
case 7: {
break;
}
default: {
System.out.println("Unknown option, choose again");
}
System.out.println("");
}
}
System.out.println("\nHave a nice day :3");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment