Created
June 16, 2020 22:14
-
-
Save DanyelMorales/df810eb5ef6fcd06e1788f23e2bc3c46 to your computer and use it in GitHub Desktop.
Different approaches to detect repeated letters
This file contains 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
package com.company; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
import static java.util.stream.Collectors.counting; | |
import static java.util.stream.Collectors.groupingBy; | |
public class Main { | |
public static void main(String[] args) { | |
Main m = new Main(); | |
System.out.println(m.containsFunctional("teraw", "water")); | |
System.out.println(m.contains("teraw", "water")); | |
System.out.println(m.containsWithMap("cbeajbe", "bebe")); | |
System.out.println(m.containsWithMapFunctional("metoetoma", "tomatoe")); | |
} | |
public boolean containsWithMapFunctional(String haystack, String needle) { | |
Stream<String> needleStream = Arrays.stream(needle.split("")); | |
Stream<String> haystackStream = Arrays.stream(haystack.split("")); | |
Map<String, Long> needleCounter = needleStream | |
.collect(groupingBy(Function.identity(), counting()) | |
); | |
Map<String, Long> haystackCounter = haystackStream.collect(groupingBy(Function.identity(), counting()) | |
); | |
return needleCounter.entrySet().stream().allMatch( | |
c -> haystackCounter.containsKey(c.getKey()) && haystackCounter.get(c.getKey()).equals(c.getValue()) | |
); | |
} | |
public boolean containsWithMap(String haystack, String needle) { | |
Map<Character, Integer> needleCounter = new HashMap<>(); | |
Map<Character, Integer> haystackCounter = new HashMap<>(); | |
for (char n : needle.toCharArray()) { | |
Integer count = 0; | |
if (needleCounter.containsKey(n)) { | |
count = needleCounter.get(n); | |
} | |
count++; | |
needleCounter.put(n, count); | |
} | |
for (char c : haystack.toCharArray()) { | |
Integer count = 0; | |
if (haystackCounter.containsKey(c)) { | |
count = haystackCounter.get(c); | |
} | |
count++; | |
haystackCounter.put(c, count); | |
} | |
Iterator<Character> needleIt = needleCounter.keySet().iterator(); | |
while (needleIt.hasNext()) { | |
Character c = needleIt.next(); | |
if (!haystackCounter.containsKey(c) || !haystackCounter.get(c).equals(needleCounter.get(c))) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public boolean containsFunctional(String haystack, String needle) { | |
return needle.chars().allMatch((a) -> haystack.indexOf(a) > -1); | |
} | |
public boolean contains(String haystack, String needle) { | |
for (char c : needle.toCharArray()) { | |
boolean flag = false; | |
for (char n : haystack.toCharArray()) { | |
if (n == c) { | |
flag = true; | |
break; | |
} | |
} | |
if (!flag) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment