class Solution {
public int numJewelsInStones(String J, String S) {
int count = 0;
for (int i = 0; i < S.length(); i++)
for (int j = 0; j < J.length(); j++)
if (S.charAt(i) == J.charAt(j))
count++;
return count;
}
}
Category Archives: Computer Science
806. Number of Lines To Write String
806. Number of Lines To Write String
class Solution { public int[] numberOfLines(int[] widths, String S) {
int currentChar = 0;
if (S.length() == 0) return new int[]{0,0};
int lines = 1;
for (int i = 0; i < S.length(); i++) {
currentChar += widths[S.charAt(i) - 97];
if( currentChar > 100){
currentChar -= widths[S.charAt(i) - 97];
currentChar = widths[S.charAt(i) - 97];
lines++;
}else if( currentChar == 99 && i < S.length() - 1 /*at least there is one following char*/){
currentChar = 0;
lines++;
}
}
return new int[] { lines, currentChar};
}}
811. Subdomain Visit Count
import java.util.Hashtable;
public static List subdomainVisits(String[] cpdomains) {
List out = new ArrayList();
Hashtable totals = new Hashtable();
for (int i = 0; i < cpdomains.length; i++) {
String[] cpDomainsSplit = cpdomains[i].split(" ");
String visits = cpDomainsSplit[0];
String cpdomain = cpDomainsSplit[1];
String[] parts = cpdomain.split("\\.");
for (int x = 0; x = 0; j--) {
String currentPart = "";
for (int k = j; k < parts.length; k++) {
currentPart += (parts[k]);
}
if (totals.get(currentPart) != null) {
int exist = totals.get(currentPart);
totals.replace(currentPart, totals.get(currentPart) + new Integer(visits));
} else
totals.put(currentPart, new Integer(visits));
}
}
for (String key : totals.keySet()) {
out.add(totals.get(key) + " " + key);
}
return out;
}
561. Array Partition I
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int result = 0;
for (int i = 0; i < nums.length; i += 2) {
result += nums[i];
}
return result;
}
}
728. Self Dividing Numbers
class Solution {
public static boolean isSelfDividing( int i){
for ( int x = i ; x > 0 ; x /=10){
int m = x % 10;
if( m == 0 /*to avoid i%0*/|| i % m != 0) return false;
}
return true;
}
public List selfDividingNumbers(int left, int right) {
List out = new ArrayList();
for ( int i = left ; i <= right ; i++ ){
if ( isSelfDividing(i)) out.add(i);
}
return out;
}
}
657. Robot Return to Origin
class Solution {
public boolean judgeCircle(String moves) {
int x = 0;
int y = 0;
for (int i = 0; i &lt; moves.length(); i++) {
char c = moves.charAt(i);
if (c == 'R') {
x++;
} else if (c == 'L') {
x--;
} else if (c == 'U') {
y--;
} else if (c == 'D') {
y++;
}
}
if (x == 0 &amp;&amp; y == 0)
return true;
else
return false;
}
}
Unique Morse Code Words
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String [] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
HashSet output = new HashSet();
for ( int i = 0 ; i < words.length ; i++){
StringBuilder transformation = new StringBuilder();
for ( int j = 0 ; j <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>< words[i].length() ; j++ ){
transformation.append(codes[words[i].charAt(j)-97]);
}
output.add(transformation.toString());
}
return output.size();
}
}
A. Games
import java.util.Scanner;
public class Games {
private static class Team {
public Team(int home, int guest) {
this.home = home;
this.guest = guest;
}
int home;
int guest;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Team[] teams = new Team[n];
for (int i = 0; i < n; i++) {
teams[i] = new Team(in.nextInt(), in.nextInt());
}
int guestUniformGames = 0;
for (int i = 0; i < teams.length; i++)
for (int j = 0; j < teams.length; j++)
if (i != j)
if (teams[i].home == teams[j].guest)
guestUniformGames++;
System.out.println(guestUniformGames);
}
}
A. Night at the Museum
import java.util.Scanner;
public class NightattheMuseum {
public static int distance(char start, char end) {
return Math.min(Math.abs(start - end), Math.abs(26 - Math.abs(start - end)));
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String data = in.nextLine();
int rotations = 0;
rotations += distance('a', data.charAt(0));
for (int i = 0; i < data.length() - 1; i++) {
rotations += distance(data.charAt(i), data.charAt(i + 1));
}
System.out.println(rotations);
}
}