Skip to content

Commit

Permalink
Longest Substring Without Repeating Characters
Browse files Browse the repository at this point in the history
  • Loading branch information
shamstabrez16 committed Oct 18, 2023
1 parent 402034e commit 5f5b11e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package String;
public class LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s) {
int pointer = 0;
int max = 0;
StringBuilder sBuilder = new StringBuilder();
while (pointer < s.length()) {
char c = s.charAt(pointer);
int index = sBuilder.indexOf(String.valueOf(c));
if (index >= 0) {
sBuilder.delete(0, index + 1);
}
sBuilder.append(c);
max = Math.max(max, sBuilder.length());
pointer++;
}
return max;

}
}
52 changes: 52 additions & 0 deletions src/test/java/TestLongestSubstringWithoutRepeatingCharacters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import String.LongestSubstringWithoutRepeatingCharacters;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestLongestSubstringWithoutRepeatingCharacters {

private LongestSubstringWithoutRepeatingCharacters repeatingCharacters;

@Before
public void setUp(){
repeatingCharacters = new LongestSubstringWithoutRepeatingCharacters();
}

@Test
public void testCase1()
{
String s = "abcabcbb";
int expectedoutput = 3;
Assert.assertEquals(expectedoutput,repeatingCharacters.lengthOfLongestSubstring(s));
}
@Test
public void testCase2()
{
String s = "bbbbb";
int expectedoutput = 1;
Assert.assertEquals(expectedoutput,repeatingCharacters.lengthOfLongestSubstring(s));
}
@Test
public void testCase3()
{
String s = "pwwkew";
int expectedoutput = 3;
Assert.assertEquals(expectedoutput,repeatingCharacters.lengthOfLongestSubstring(s));
}
//aab
@Test
public void testCase4()
{
String s = "aab";
int expectedoutput = 2;
Assert.assertEquals(expectedoutput,repeatingCharacters.lengthOfLongestSubstring(s));
}
//dvdf
@Test
public void testCase5()
{
String s = "dvdf";
int expectedoutput = 3;
Assert.assertEquals(expectedoutput,repeatingCharacters.lengthOfLongestSubstring(s));
}
}

0 comments on commit 5f5b11e

Please sign in to comment.