Skip to content

Instantly share code, notes, and snippets.

@mllopart
Last active March 21, 2021 06:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mllopart/842a498cded11648f6ea54104573bbbe to your computer and use it in GitHub Desktop.
Save mllopart/842a498cded11648f6ea54104573bbbe to your computer and use it in GitHub Desktop.
Substring Calculator HackerRank test
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
// your code goes here
// your code goes here
string s = "kincenvizh";
Dictionary<string, int> substrings = new Dictionary<string, int>();
int count = 0;
for(int i = 0; i < s.Length; i++)
{
string sub = s.Substring(i);
for(int j = 0; j < sub.Length; j++)
{
string subDistinct = sub.Substring(0, sub.Length - j);
if(!substrings.ContainsKey(subDistinct))
{
substrings.Add(subDistinct, 1);
Console.WriteLine(subDistinct);
count += 1;
}
else
{
continue;
}
}
}
Console.WriteLine(count);
}
}
<?php
$s = "kincenvizh";
$count_num = 0;
$first = true;
$array_chars = array();
for ($x = 0; $x <= strlen($s); $x++) {
if (!$first)
$s_treat = substr($s,$x,strlen($s));
else {
$s_treat = $s;
$first = false;
}
if (!in_array($s_treat,$array_chars )) {
array_push($array_chars, $s_treat);
$count_num++;
}
print('pri: '.$s_treat);
print_r($array_chars);
for ($xx = 0; $xx <= strlen($s_treat)-1; $xx++) {
print substr('sec: '.$s_treat,0,-$xx)."\n";
$count_num++;
}
}
echo $count_num;
def substringCalculator(st)
collection = Array.new
count = 0
string = st
(0..string.length).each do |i|
temp = string
(0..string.length).each do |i|
if temp.length > 0
if collection.include?(temp)
puts "#{temp} already exists."
else
number = count +1
collection << temp
puts "#{number.to_s} #{temp}"
count = count + 1
end
temp = temp.chop
end
end
string = string[1..-1]
end
puts "Total: #{count}"
end
substringCalculator('kincenvizh')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment