More Exercises
Longest Substring without repeating characters.
Example 1:
Input: abcabcdbb
Output: abcd
Example 2:
Input: aaaaa
Output: a
Solution:
public class Main {
public static String longestNonRepeating(String str){
String temp = "";
String longest = "";
for (int i = 0; i < str.length(); i++) {
char current_char = str.charAt(i);
//if in string
//indexOf: Returns -1 if not found
if (temp.indexOf(current_char) == -1){
temp += current_char;
} else { //if in string
if (temp.length() > longest.length())
longest = temp;
temp = ""; //Reset the string
}
}
return longest.length()>temp.length() ? longest : temp; //Since it temp was always the longest
}
public static void main(String[] args) {
String res = longestNonRepeating("pickoutthelongestsubstring");
System.out.print("substring: " + res);
System.out.println(", length: " +res.length());
}
}
substring: ubstring, length: 8
Tests
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static com.company.Main.longestNonRepeating;
public class MyTests {
@Test
void TestLongestSubsequence(){
assertEquals("abc", longestNonRepeating("abcabcbb"));
assertEquals("b", longestNonRepeating("bbbbb"));
boolean ifEmpty = longestNonRepeating("").length() == 0;
assertTrue(ifEmpty);
String res = longestNonRepeating("pickoutthelongestsubstring");
assertEquals("ubstring", res);
}
}
Tests passed: 1 of 1 test
Length of each word
Given a string s
containing a string seperated by spaces calculate the length of each word
Example 1:
Input: s = "hello friends"
Output: 5 7
Example 2:
Input: s = "my name is nissan"
Output: 2 4 2 6
Solution
public class Main {
public static int[] lenOfEachWord(String str){
// String[] strings = str.split(" ");
String[] strings = str.split("\\s+");
int[] lens = new int[strings.length];
for (int i = 0; i < strings.length; i++)
lens[i] = strings[i].length();
return lens;
}
public static void main(String[] args) {
int[] lens = lenOfEachWord("hello world my name");
for (int i = 0; i < lens.length; i++)
System.out.println(lens[i]);
}
}
Longest substring appears at both ends of a given string
Example 1:
Input: playersplay
Output: play
Example 2:
Input: 1234abc123
Output: 123
Example 2:
Input: helloworld
Output:
Solution
public class Main {
public static String appearAtBothEnds(String str) {
int len = str.length();
String front_and_back_str = "";
String tmp = "";
for (int i = 0; i < len; i++) {
tmp += str.charAt(i);
int temp_len = tmp.length();
String sub = str.substring(len-temp_len,len);
if (i < len / 2 && tmp.equals(sub))
front_and_back_str = tmp;
}
return front_and_back_str;
}
public static void main (String[] args) {
String str = "playersplay";
System.out.println("string is: "+str);
System.out.println("The longest substring is: "+appearAtBothEnds(str));
}
}
tests
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static com.company.Main.appearInBothEnds;
public class MyTests {
@Test
void TestAppearAtBothEnds(){
assertEquals("play", appearAtBothEnds("playersplay"));
assertEquals("123", appearAtBothEnds("1234abc123"));
assertEquals("", appearAtBothEnds("helloworld"));
}
}
Tests passed: 1 of 1 test
Last updated