AllUnique |
Tests |
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean isAllUnique(String s) boolean[] check = new boolean[256]; for(char c : s.toCharArray()) {> if(check[c])> {> return false;> } check[c] = true; }> return true;>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean isAllUniqueWithStreams(String s) SetCharacter set = s.chars().mapToObj(c - (char)c).collect(Collectors.toSet());> return set.size() == s.length();>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static int lengthOfLongestUniqueSubstr(String s) int max = 0; for(int i = 0; i s.length(); ++i) {> for(int j = s.length(); j = i; --j)> { > String subStr = s.substring(i, j);> if(isAllUnique(subStr) && subStr.length() max)> { > max = subStr.length();> }> } } return max;>} |
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass() String allUnique = "abcdefghijklmnopq"; String notAllUnique = "abcdefghidjklm";> boolean check = isAllUnique(allUnique); > if(!check) { return false;> }> check = isAllUnique(notAllUnique); > if(check) { return false;> }> check = isAllUniqueWithStreams(allUnique);> if(!check) { return false;> }> check = isAllUniqueWithStreams(notAllUnique);> if(check)> { return false; }> String test = "qwertyuiopqazxcvbnm"; check = lengthOfLongestUniqueSubstr(test) == 18; if(!check)> {> return false;> } return true; } |
Anagrams
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean isAnagram(String s1, String s2)if(s1.length() != s2.length()){> return false;> }> int[] check = new int[256];> for(char c : s1.toCharArray())> {check[c]++;}> for(char c : s2.toCharArray())> {> if(--check[c] 0)> {> return false;> }}return true;>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean isAnagramWithSort(String s1, String s2)> if(s1.length() != s2.length())> {return false;}> char[] a1 = s1.toCharArray();> char[] a2 = s2.toCharArray();> Arrays.sort(a1); > Arrays.sort(a2);> return Arrays.equals(a1, a2);>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static MapString, SetString groupAnagrams(String[] a)> MapString, SetString result = new HashMap();> for(String s : a)> {> String sortedString = sortString(s); > result.computeIfAbsent(sortedString, x - new HashSet()).add(s);> }> return result;>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static MapString, SetString groupAnagramsWithStreams(String[] a)> ListString list = Arrays.stream(a).collect(Collectors.toList());> return list.stream().collect(Collectors.groupingBy(e - sortString(e), Collectors.toSet()));>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String s1 = "abcdefg", s2 = "gafbedc", s3 = "abcgfee";pre data-role="codeBlock" data-info="js" class="language-javascript"> boolean check = isAnagram(s1, s2);> if(!check){return false;> }> check = isAnagram(s1, s3);> if(check){return false;> }> check = isAnagramWithSort(s1, s2); > if(!check){return false;> }> check = isAnagramWithSort(s1, s3); > if(check){return false;> }> String[] strs = {"abc", "acb", "bca", "xyz", "zyx", "yxz"};MapString, SetString anagrams = groupAnagrams(strs);pre data-role="codeBlock" data-info="js" class="language-javascript"> SetString s = anagrams.get("abc");> if(s.size() != 3 || !(s.contains("abc") && s.contains("acb") && s.contains("bca")))> {return false;}> s = anagrams.get("xyz");> if(s.size() != 3 || !(s.contains("xyz") && s.contains("zyx") && s.contains("yxz")))> {> return false; > }anagrams = groupAnagramsWithStreams(strs);s = anagrams.get("abc");if(s.size() != 3 || !(s.contains("abc") && s.contains("acb") && s.contains("bca"))){> return false;> }s = anagrams.get("xyz");if(s.size() != 3 || !(s.contains("xyz") && s.contains("zyx") && s.contains("yxz"))){> return false;> }return true;}
CompareVersions
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static int compareVersions(String v1, String v2)String[] a1 = v1.split("\\.");String[] a2 = v2.split("\\.");int i = 0;while(i a1.length || i a2.length){> if(i a1.length && i a2.length){> int v = Integer.parseInt(a1[i]) - Integer.parseInt(a2[i]);if (v 0){> return 1;> }> if (v 0) > {> return -1;> }> }else if(i a1.length){> if(Integer.parseInt(a1[i]) 0)> {> return 1; > }> }else if(i a2.length){> if(Integer.parseInt(a2[i]) 0)> {> return -1;> }> }i++;}> return 0;>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = compareVersions("1.1.2.0", "1.1.2") == 0;pre data-role="codeBlock" data-info="js" class="language-javascript"> if(!check){return false;> }> check = compareVersions("1.1.2.0.4", "1.1.2.1") == -1;> if(!check)> {> return false;> }check = compareVersions("1.1.2.3.4", "1.1.2.3") == 1;pre data-role="codeBlock" data-info="js" class="language-javascript"> if(!check){return false;> }> return true;}
CountAndSay
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String countAndSay(int n)String s = String.valueOf(n);char[] a = s.toCharArray();StringBuilder sb = new StringBuilder();int count = 1;> for(int i = 1; i a.length; ++i)> {> if(a[i] == a[i - 1]) > {> count++;> }else{> sb.append(count);> sb.append(a[i - 1]); > count = 1;> }}sb.append(count);> sb.append(a[a.length - 1]);> return sb.toString();>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = countAndSay(121).equals("111211");> if(!check){return false;> }> return true;}
CountWords
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static int countWordsSeparatedMyMultiplesSpaces(String s)> int count = 0;> boolean lastSpace = false;> for(char c : s.toCharArray())> {if(Character.isWhitespace(c)){lastSpace = true;> }> else> { > if(lastSpace)> {> count++;> }> lastSpace = false;> }}if(!lastSpace)> {> count++;> }return count;}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static int countWordsWithSplit(String s)return s.split("\\s+").length;} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String test = "this is a t es t";boolean check = countWordsSeparatedMyMultiplesSpaces(test) == 6;pre data-role="codeBlock" data-info="js" class="language-javascript"> if(!check){return false;> }> check = countWordsWithSplit(test) == 6; > if(!check){return false;> }> return true;}
Encode
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String encode(String s)// aaabbbbbcdddd - a3b5c1d4if(encodeLength(s) = s.length()){return s;> }> char[] a = s.toCharArray();> char last = a[0];> int count = 1;> StringBuilder sb = new StringBuilder();> for(int i = 1; i a.length; ++i)> {if(a[i] == last){> count++;> }> else> {> sb.append(last);> sb.append(count);> count = 1;> last = a[i]; > }}sb.append(last);> sb.append(count);> return sb.toString();>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String decode(String s)// a3b12c2 - aaabbbbbbbbbbbbccString[] letters = s.split("[1-9]+");String[] numbers = s.split("[a-zA-Z]+");// numbers need to be shifted by 1numbers = Arrays.copyOfRange(numbers, 1, numbers.length);> StringBuilder sb = new StringBuilder(); > for(int i = 0; i letters.length; ++i)> {int n = Integer.parseInt(numbers[i]);char[] a = new char[n];Arrays.fill(a, letters[i].charAt(0));sb.append(a);}> return sb.toString();>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>private static int encodeLength(String s)char[] a = s.toCharArray();char last = a[0];int count = 1, size = 0;> for(int i = 1; i a.length; ++i)> {> if(a[i] == last)> { > count++;> }else{> size += 1 + String.valueOf(count).length();> count = 1;> last = a[i];> }}size += 1 + String.valueOf(count).length();return size;} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = encode("aaabbbbbcdddd").equals("a3b5c1d4");pre data-role="codeBlock" data-info="js" class="language-javascript"> if(!check){return false;> }> check = decode("a3b5c1d4").equals("aaabbbbbcdddd");> if(!check)> {> return false;> }return true;}
FirstUnique
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>public static char firstUnique(String s)// O(n*n) complexityfor(int i = 0; i s.length(); ++i){> boolean repeated = false;> for(int j = 0; j s.length(); ++j)> { > if(i != j && s.charAt(i) == s.charAt(j))> {> repeated = true;> break;> }> }if(!repeated){> return s.charAt(i);> }> }return 0;}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>public static char firstUniqueUsingSet(String s)// O(n) complexitySetCharacter set = new HashSet();boolean[] repeated = new boolean[256];for(char c : s.toCharArray()){> if (!repeated[c])> {> if (!set.contains(c)) > {> set.add(c);> }> else> { > set.remove(c);> repeated[c] = true;> }> }}return set.size() 0 ? set.iterator().next() : 0;}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>public static char firstUniqueUsingMap(String s)MapCharacter, Integer charFrequency = new HashMap();pre data-role="codeBlock" data-info="js" class="language-javascript"> for(char c : s.toCharArray())> {charFrequency.merge(c, 1, Integer::sum);}for(char c : s.toCharArray())> {> if(charFrequency.get(c) == 1) > {> return c;> }}return 0;>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = firstUnique("abcdabc") == 'd';> if(!check){return false;> }> check = firstUnique("abcdefg") == 'a';> if(!check)> {return false;}> check = firstUnique("abcddcba") == 0;if(!check)> {> return false;> }check = firstUniqueUsingSet("abcdabc") == 'd';> if(!check){return false;> }> check = firstUniqueUsingSet("abcdefg") == 'a';> if(!check)> {return false;}> check = firstUniqueUsingSet("abcddcba") == 0;if(!check)> {> return false;> }check = firstUniqueUsingMap("abcdabc") == 'd';> if(!check){return false;> }> check = firstUniqueUsingMap("abcdefg") == 'a';> if(!check)> {return false;}> check = firstUniqueUsingMap("abcddcba") == 0;if(!check)> {> return false;> }return true;}
LongestUniformSubstring
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>public static int[] longestUniform(String s)// for the input: "abbbccda" the longest uniform substring is "bbb"pre data-role="codeBlock" data-info="js" class="language-javascript"> int max = 0, count = 1, startIdx = 0;> char lastChar = s.charAt(0);> for(int i = 1; i s.length(); ++i)> {> if(lastChar == s.charAt(i)) > { > count++;> }else{> if(count max)> {> max = count;> startIdx = i - count;> }> lastChar = s.charAt(i); > count = 1;> }}if(count max)> {> return new int[] {s.length() - count, count};> }> return new int[] {startIdx, max};>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = Arrays.equals(new int[] {1, 3}, longestUniform("abbbccda")) &&pre data-role="codeBlock" data-info="js" class="language-javascript"> Arrays.equals(new int[] {29, 8}, longestUniform("1000011010101110110100010010011111111"));> if(!check){return false;> }> return true;}
LongestWordWithAllLetters
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String longestWordWithAllLetters(String letters, String[] dictionary)> int maxLen = 0, len = 0;> String longestWord = "";> for(String s : dictionary)> {for(char c : s.toCharArray()){if(letters.indexOf(c) != -1)> {> len++;> } > else > {> len = 0;> break;> }> }if(len 0){> if(len maxLen)> {> maxLen = len; > longestWord = s; > }> }}return longestWord;>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>public static String longestWordWithAllLettersWithStreams(String letters, String[] dictionary)> MapBoolean, ListString map = Arrays.stream(dictionary).collect(Collectors.groupingBy(s - s.chars().allMatch(x - letters.indexOf(x) != -1)));pre data-role="codeBlock" data-info="js" class="language-javascript"> PairString,Integer longest = map.get(true).stream() > .map(p - Pair.of(p, p.length())).max(Comparator.comparing(Pair::getRight))> .get();> return longest.getLeft(); >} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = longestWordWithAllLetters("oet",> new String[] {"to", "toe", "tooeet", "toes"}).equals("tooeet");> if(!check){return false;> }> check = longestWordWithAllLettersWithStreams("oet",> new String[] {"to", "toe", "tooeet", "toes"}).equals("tooeet");> if(!check)> {return false;}> return true;>}
Palindrome
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean isPalindrome(String s)char[] a = s.toCharArray();int left = 0, right = a.length -1;while(left right){> if(a[left++] != a[right--])> {> return false;> }}return true;>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static int lengthOfLongestPalindrome(String s)int max = 1;for(int i = 0; i s.length(); ++i){> for(int j = s.length(); j = i; --j)> { > String subStr = s.substring(i, j);> if(isPalindrome(subStr) && subStr.length() max)> { > max = subStr.length();> }> }}return max;>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String longestPalindrome(String s)String longest = "";for(int i = 0; i s.length(); ++i){> for(int j = s.length(); j = i; --j){> String subStr = s.substring(i, j);> if(isPalindrome(subStr) && subStr.length() longest.length()){> longest = subStr;> }> }> }return longest;}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String makeShortestPalindrome(String s)// Given a string S, you are allowed to convert it to a palindrome by> // adding characters in front of it. > // "cdcba" - "abcdcba"> int start = 0, end = s.length() - 1;> while(start end)> {if(s.charAt(start) == s.charAt(end)){start++;> end--;> }> else> {> end--; > }}if(start == s.length() / 2)> {> return s;> }String suffix = s.substring(start + 1);String prefix = new StringBuilder(suffix).reverse().toString();> return prefix + s.charAt(start) + suffix;>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String s1 = "abcddcba", s2 = "abcdcba", s3 = "abcdba";pre data-role="codeBlock" data-info="js" class="language-javascript"> boolean check = isPalindrome(s1);> if(!check){return false;> }> check = isPalindrome(s2);> if(!check){return false;> }> check = isPalindrome(s3);> if(check){return false;> }> check = lengthOfLongestPalindrome("dfrabcdedcbaprewsg") == 9;> if(!check)> {> return false;> }check = longestPalindrome("dfrabcdedcbaprewsg").equals("abcdedcba");pre data-role="codeBlock" data-info="js" class="language-javascript"> if(!check)> {return false;}> check = makeShortestPalindrome("cdcba").equals("abcdcba");> if(!check){return false;> }> return true;}
Pangram
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>The sentence "The quick brown fox jumps over the lazy dog" contains>every single letter in the alphabet. Such sentences are called pangrams.Write a function findMissingLetters, which takes a String `sentence`,>and returns all the letters it is missing (which prevent it from being a pangram).>You should ignore the case of the letters in sentence, and your return should be all lower case letters,>in alphabetical order.data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>private static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";> >public static String missingLetters(String s)SortedSetCharacter missingSet = new TreeSet(ALPHABET.chars().mapToObj(c - (char) c).collect(Collectors.toSet()));> String lower = s.toLowerCase(); > return missingSet.stream()> .filter(a - lower.indexOf(a) == -1)> .map(Object::toString).collect(Collectors.joining());>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()> boolean check = "".equals(missingLetters("The quick brown fox jumps over the lazy dog")) &&> "abcdefghijklmnopqrstuvwxyz".equals(missingLetters(""));> if(!check)> {return false;}> return true;>}
PatternMatch
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean patternMatch(String s, String pattern)> int strLen = s.length(), patLen = pattern.length();pre data-role="codeBlock" data-info="js" class="language-javascript"> if(strLen patLen)> {return false;}> if(strLen == patLen)> {> return s.equals(pattern);> }for(int i = 0; i strLen - patLen; ++i){if(s.charAt(i) == pattern.charAt(0)){> int j;> for(j = 0; j patLen; ++j) > {> if(s.charAt(i + j) != pattern.charAt(j))> { > break;> } > }> if(patLen == j)> {> return true; > }> }}return false;>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean subsequenceMatch(String s, String pat)> // Following cases will match:> // String Pattern> // "abcxdefyzlmn" "xyz"> // "abcxdefxyzlmn" "xyz" > int patIdx = 0; > for(int i = 0; i s.length() && patIdx pat.length(); ++i){> if(s.charAt(i) == pat.charAt(patIdx))> {> patIdx++; > }}return patIdx == pat.length();>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String test = "Hello world", pat1 = "o wo", pat2 = "owo";pre data-role="codeBlock" data-info="js" class="language-javascript"> boolean check = patternMatch(test, pat1);> if(!check)> {return false;}> check = patternMatch(test, pat2);> if(check)> {> return false; > }check = subsequenceMatch("abcxdefyzlmn", "xyz");> if(!check){return false;> }> check = subsequenceMatch("abcxdefxyzlmn", "xyz");> if(!check)> {> return false;> }return true;}
Remove
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String removeChars(String s, String remove)boolean[] charsToRemove = new boolean[256];for(char c : remove.toCharArray()){> charsToRemove[c] = true;> }> int pos = 0;> char[] a = s.toCharArray();> for(char c : a)> {if(!charsToRemove[c]){> a[pos++] = c;> }> }return new String(a, 0, pos);}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String removeCharsWithStreams(String s, String remove)> return s.chars().filter(x - remove.indexOf(x) == -1)pre data-role="codeBlock" data-info="js" class="language-javascript"> .mapToObj(c - (char)c).map(String::valueOf).collect(Collectors.joining());>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String[] removeStringFromArrayWithStreams(String[] input, String value)> return Arrays.stream(input).filter(e - !e.equals(value)).toArray(String[]::new);}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String removeDups(String s)if(s == null || s.length() 2){> return s;> }> char[] a = s.toCharArray();> int pos = 1;> for(int i = 1; i a.length; ++i)> {int j;for(j = 0; j pos; ++j){> if(a[i] == a[j])> {> break; > }> }if(j == pos){> a[pos++] = a[i];> }> }return new String(a, 0, pos);}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String removeDupsWithStreams(String s)return s.chars().mapToObj(c - (char)c).distinct()> .map(String::valueOf).collect(Collectors.joining());>}data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String removeAdjacentDups(String s)char[] a = s.toCharArray();int pos = 1;> for(int i = 1; i a.length; ++i)> {> if(a[i] != a[i - 1]) > {> a[pos++] = a[i];> }}return new String(a, 0, pos);>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String s = "Kill all vowels";String remove = "aeiou ";String result = removeChars(s,remove);boolean check = result.equals("Kllllvwls");if(!check){> return false;> }> result = removeCharsWithStreams(s,remove);> check = result.equals("Kllllvwls");> if(!check)> {return false;}> String [] input = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};> String[] output = removeStringFromArrayWithStreams(input, "e");> check = Arrays.equals(output, new String[] {"a", "b", "c", "d", "f", "g", "h", "i"});> if(!check)> {return false;}> check = removeDups("abcdacbdabcd").equals("abcd");> if(!check){return false;> }> check = removeDupsWithStreams("abcdacbdabcd").equals("abcd");if(!check)> {> return false;> }check = removeAdjacentDups("aaabbbbcccb").equals("abcb");pre data-role="codeBlock" data-info="js" class="language-javascript"> if(!check){return false;> }> return true;}
ReverseWords
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static String reverseWords(String sentence)Character[] a = sentence.chars().mapToObj(c - (char)c).toArray(Character[]::new);pre data-role="codeBlock" data-info="js" class="language-javascript"> reverse(a);> sentence = Arrays.stream(a).map(String::valueOf).collect(Collectors.joining());> String[] words = sentence.split("\\s+");reverse(words);> return Arrays.stream(words).collect(Collectors.joining(" "));>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String s = "one two three";return reverseWords(s).equals("eno owt eerht");>}
Rotation
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean isRotation(String s1, String s2)if(s1.length() != s2.length()){> return false;> }> String s3 = s1 + s1;> return s3.contains(s2); >} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()String s1 = "abcdefgh";String s2 = "cdefghab";String s3 = "hgfedcba";boolean check = isRotation(s1, s2);if(!check)> {> return false;> }check = isRotation(s2, s1);if(!check)> {> return false;> }check = isRotation(s1, s3);if(check)> {> return false;> }return true;}
WordDistance
Tests
data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static int wordDistance(String[] words, String word1, String word2)> // words: ["one", "two", "three", "four", "five"]// word1 = "two", word2 = "five" == distance = 3> // word1 = "four", word2 = "two" == distance = 2> int pos1 = -1;> int pos2 = -1;> int min = Integer.MAX_VALUE;> for(int i = 0; i words.length; ++i)> {String w = words[i];if(w.equals(word1)){> pos1 = i;> if(pos2 != -1)> {> min = Math.min(min, pos1 - pos2);> }> }else if(w.equals(word2)){> pos2 = i;> if(pos1 != -1)> {> min = Math.min(min, pos2 - pos1);> }> }}return min;>} data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme=Confluence>static boolean testsPass()boolean check = wordDistance(new String[]{"one", "two", "three", "four", "five"},"two", "five") == 3;if(!check){> return false;> }> check = wordDistance(new String[]{"one", "two", "three", "four", "five"}, "four", "two") == 2;> if(!check)> {return false;}> return true;>}