Skip to content

Instantly share code, notes, and snippets.

@backslash112
Last active October 4, 2018 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backslash112/8c5b1ee68686cbbfddd391609c3a90a2 to your computer and use it in GitHub Desktop.
Save backslash112/8c5b1ee68686cbbfddd391609c3a90a2 to your computer and use it in GitHub Desktop.
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.println(modifyStr("").equals(""));
System.out.println(modifyStr("Automotive parts").equals("A6e p3s"));
System.out.println(modifyStr("Automotive par").equals("A6e p1r"));
System.out.println(modifyStr("Automotive pa").equals("A6e p0a"));
System.out.println(modifyStr("Automotive p").equals("A6e p0p"));
System.out.println(modifyStr("**Automotive p").equals("**A6e p0p"));
System.out.println(modifyStr("*Automotive* p").equals("*A6e* p0p"));
}
static String modifyStr(String input) {
// empty check
if (input == null || input.equals("")) return input;
String output = "";
int wordStart = 0;
int delimit = 0;
while (wordStart < input.length()) {
// find the start index of word
while (!Character.isAlphabetic(input.charAt(wordStart))) {
output += input.charAt(wordStart);
wordStart++;
}
// find the delimit index of word
delimit = wordStart + 1;
while (delimit < input.length() &&
Character.isAlphabetic(input.charAt(delimit))) {
delimit++;
}
// find the count of distinct letters
HashSet<Character> set = new HashSet<>();
for (int i = wordStart+1; i < delimit-1; i++) {
set.add(input.charAt(i));
}
output += String.valueOf(input.charAt(wordStart)) + set.size() +
String.valueOf(input.charAt(delimit-1));
wordStart = delimit;
}
return output;
}
}
@backslash112
Copy link
Author

PP 1.6: In the language of your choice, write a method that modifies a string using the following rules:

  1. Each word in the input string is replaced with the following: the first letter of the word, the count of
    distinct letters between the first and last letter, and the last letter of the word. For example,
    “Automotive parts" would be replaced by "A6e p3s".
  2. A "word" is defined as a sequence of alphabetic characters, delimited by any non-alphabetic
    characters.
  3. Any non-alphabetic character in the input string should appear in the output string in its original
    relative location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment