Compare commits

...

6 Commits

Author SHA1 Message Date
Rhinemann ca2b85ef9e . 2023-05-07 17:09:03 +03:00
Rhinemann 77f96fafa6 Working model of Lab 3. 2023-05-07 17:07:56 +03:00
Rhinemann 084f131c90 . 2023-05-07 16:46:18 +03:00
Rhinemann 197426e266 Initial lab 3. 2023-05-07 15:19:23 +03:00
Rhinemann 661c3641a1 Naming changes. 2023-05-07 15:19:06 +03:00
Rhinemann fc5704dd15 Cargo.toml added release profile. 2023-05-07 14:08:43 +03:00
5 changed files with 69 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import java.util.Scanner; import java.util.Scanner;
public class Lab_1 { public class lab_1 {
public static int protectedInput(String variable_to_read, Scanner input) { public static int protectedInput(String variable_to_read, Scanner input) {
int read_variable; int read_variable;

View File

@ -1,6 +1,6 @@
import java.util.Scanner; import java.util.Scanner;
public class Lab_2 { public class lab_2 {
public static short protectedInput(String inputPrompt, String errorMessage, Scanner input) { public static short protectedInput(String inputPrompt, String errorMessage, Scanner input) {
short read_variable; short read_variable;

58
Java/lab_3/lab_3.java Normal file
View File

@ -0,0 +1,58 @@
package OOP.Java.lab_3;
public class lab_3 {
static int maxStrLength;
static String result;
public static void cSubUtil(StringBuilder stringbuilder, int leftBoundary, int rightBoundary) {
String string = stringbuilder.toString().toLowerCase().replaceAll("[^a-z]","");
// check if the indices lie in the range of string
// and also if it is palindrome
while (leftBoundary >= 0 && rightBoundary < string.length() && string.toLowerCase().charAt(leftBoundary) == string.toLowerCase().charAt(rightBoundary)) {
// expand the boundary
leftBoundary--;
rightBoundary++;
}
// if it's length is greater than maxLength update
// maxLength and res
if (rightBoundary - leftBoundary - 1 >= maxStrLength) {
result = string.substring(leftBoundary + 1, rightBoundary);
maxStrLength = rightBoundary - leftBoundary - 1;
}
return;
}
public static int longestPalSubstr(StringBuilder string)
{
result = "";
maxStrLength = 1;
// for every index in the string check palindromes
// starting from that index
for (int i = 0; i < string.length(); i++) {
// check for odd length palindromes
cSubUtil(string, i, i);
// check for even length palindromes
cSubUtil(string, i, i + 1);
}
System.out.println("Longest palindrome substring is: " + compareStrings(string));
return compareStrings(string).length();
}
public static String compareStrings(StringBuilder builder) {
for (int leftBoundary = 0; leftBoundary <= builder.length(); leftBoundary++) {
for (int rightBoundary = builder.length(); leftBoundary >= 0; rightBoundary--) {
if (result.equals(builder.substring(leftBoundary, rightBoundary).toString().toLowerCase().replaceAll("[^a-z]",""))) {
return builder.substring(leftBoundary, rightBoundary).toString();
}
}
}
return "";
}
public static void main(String[] args) {
StringBuilder stringToDetect = new StringBuilder("Eva, can I see bees in a cave?");
System.out.println("Initial string: " + stringToDetect.toString());
System.out.println("Length is: " + longestPalSubstr(stringToDetect));
}
}

View File

@ -5,5 +5,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
opt-level = 'z' # Optimize for size
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*
[dependencies] [dependencies]
text_io = "0.1.12" text_io = "0.1.12"