Last active
January 4, 2018 08:40
-
-
Save clarkdo/d06004bc85abadd0b16885cb0def2af6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class NoRepeating { | |
public static void main(String[] args) { | |
System.out.println(find(null)); | |
System.out.println(find("")); | |
System.out.println(find("111111")); | |
System.out.println(find("aaabbbcdeff")); | |
System.out.println(find("abcdeefghijjjklmno")); | |
} | |
public static String find(String str) { | |
if (null == str || str == "") return ""; | |
List<String> result = new ArrayList<>(); | |
StringBuilder builder = new StringBuilder(); | |
char[] chars = str.toCharArray(); | |
char last = 0; | |
char current; | |
for (int i = 0; i < chars.length; i++) { | |
current = chars[i]; | |
if (current == last) { | |
if (builder.length() > 0) { | |
builder.delete(builder.length() - 1, builder.length()); | |
if (builder.length() > 0) { | |
result.add(builder.toString()); | |
builder.delete(0, builder.length()); | |
} | |
} | |
} else { | |
builder.append(chars[i]); | |
} | |
last = chars[i]; | |
if (i == chars.length -1 && builder.length() > 0) { | |
result.add(builder.toString()); | |
} | |
} | |
if (result.size() == 0) return ""; | |
Collections.sort(result, (a, b) -> b.length() - a.length()); | |
return result.get(0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class Repeating { | |
public static void main(String[] args) { | |
System.out.println(find(null)); | |
System.out.println(find("")); | |
System.out.println(find("111111")); | |
System.out.println(find("aaabbbcdef")); | |
System.out.println(find("aaabbbcdeffffff")); | |
System.out.println(find("abcdeefghijjjklmno")); | |
} | |
public static String find(String str) { | |
if (null == str || str == "" || str.length() == 1) return str; | |
List<String> result = new ArrayList<>(); | |
StringBuilder builder = new StringBuilder(); | |
char[] chars = str.toCharArray(); | |
builder.append(chars[0]); | |
for (int i = 1; i < chars.length; i++) { | |
if (chars[i] != chars[i-1]) { | |
if (builder.length() > 0) { | |
result.add(builder.toString()); | |
builder.delete(0, builder.length()); | |
} | |
} | |
builder.append(chars[i]); | |
if (i == chars.length -1) { | |
result.add(builder.toString()); | |
} | |
} | |
if (result.size() == 0) return ""; | |
Collections.sort(result, (a, b) -> b.length() - a.length()); | |
return result.get(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment