Recursion with strings
Hallo,
I have to write a method that get's two String type strings (s1, s2) and returns the length of the the biggest continuous sub matrix in the matrix s2 that's all it's digits are present in matrix s1.
For example: if s1 is "xyz" and s2 is "abxyryxzycx" the method will return 4 because the length of sub matrix "yxzy" is 4 and it's the largest sub matrix of s2 that contains only digits from s1(the sub matrices are: "xy", "yxzy", "x").
I have to write this method with recursion and I'm not allowed to use any strings.
When I'm using the defined methods of the String class I can use only the following one's:
public char charAt (int i)
public int indexOf (int ch)
public int length()
public String substring (int i)
I can's see right now how I use "if" conditions with the String methods I'm allowed to use and I can't see the process of solving the problem(should I use linear search?).
Thank's to everybody that can help.
Edited by: ArikG on Jan 26, 2009 10:38 AM
I suppose I have to use linear search, but I can't use it here because I'm not allowed to use loops.
My idea is that if the linear search in s2 a digit from s1 is found so I use the method: public char charAt(int i) and from this point I don't know what to do, and I don't know if it's possible to use linear search in recursion(I know how to do it with loops) and how to write everything here(I am familiar only with the String length methods because we weren't taught in the course about the other Sting methods)
Similar Messages
-
Urgent!!!!!! Issue with String in CO
Hi All,
I am learner of OAF.
I have issue with string while converting into double.
In AM,
public String ComputeMarginPercent()
retrun form.format((55570.0*100)/184.64999999999418);
in CO..I am getting the value:
String s1 = (String)oapagecontext.getApplicationModule(oawebbean).invokeMethod("computeMarginPercent");
here getting value of s1=30,094.77
I need to convert s1 to double, while converting double its raising error.
how can I remove comman in (30,094.77) this number..?
please advise me ..Hi Pratap,
I am new to java ..
Can you please tell me in step by step..?
Thanks in advance
I tried like /..
String str= "30,094.77 ";
char[] chars = str.toCharArray();
String s = String.copyValueOf(chars);
System.out.println(s);
Still displaying value as 30,094.77 (with comma)
Edited by: user633508 on Nov 21, 2008 2:55 AM -
I am new to Java Programming.
I have a line of code that works and does what is supposed to.
faceData.getProfile("Lisa").removeFriend("Curtis");
If I assign the strings to variables such as-
String name = "Lisa";
String fName = "Curtis";
and then plug those into the same line of code, it does not work
faceData.getProfile(name).removeFriend(fName);
What could be causing the problem?
I even added some lines to print out what is stored in the variables to verify that they are what they should be, but for some reason the variables do not work while putting the strings in quotes does. Any ideas?I guarantee that something about your assertions are incorrect. Those variables are either not equal to the values you claim, or something else is going on. But it's not a problem with string variables versus string constants.
Edit: My best guess in lack of a real example from you, is that the strings in question have non-printable characters in them, such as trailing spaces or line feeds. -
Little problem with Strings.
I have an little problem with Strings, i make one comparision like this.
String nombre="Javier";
if( nombre.equalsIgnoreCase(output.getStringValue("CN_NOMBRESf",null)) )
Wich output.getStringValue("CN_NOMBRESf",null) is "Javier" too, because I display
this before and are equals.
What I do wrong?.
You are actually making your users key in things like
"\026"? Not very user-friendly, I would say. But
assuming that is the best way for you to get your
input, or if it's just you doing the input, the way to
change that 4-character string into the single
character that Java represents by '\026', you would
use a bit of code like this:char encoded =
(char)Integer.parseInt(substring(inputString, 1),
16);
DrClap has the right idea, except '\026' is octal, not hex. So change the radix from 16 to 8. Unicode is usually represented like '\u002A'. So it looks like you want:String s = "\\077";
System.out.println((char)Integer.parseInt(s.substring(1), 8));Now all you have to do is parse through the String and replace them, which I think shouldn't be too hard for you now :) -
Regex with strings that contain non-latin chars
I am having difficulty with a regex when testing for words that contain non-latin characters (specifcally Japanese, I haven't tested other scripts).
My code:
keyword = StringUtil.trim(keyword);
//if(keywords.indexOf(keyword) == -1)
regex = new RegExp("\\b"+keyword+"\\s*;","i");
if(!regex.test(keywords))
{Alert.show('"'+keywords+'" does not contain "'+keyword+'"'); keywords += keyword + "; ";}
Where keyword is
日本国
and keywords is
Chion-in; 知恩院; Lily Pond; Bridge; 納骨堂; Nōkotsu-dō; Asia; Japan; 日本国; Nihon-koku; Kansai region; 関西地方; Kansai-chihō; Kyoto Prefecture; 京都府; Kyōto-fu; Kyoto; Higashiyama-ku; 東山区; Places;
When the function is run, it will alert that keywords does not contain keyword, even though it does:
"Chion-in; 知恩院; Lily Pond; Bridge; 納骨堂; Nōkotsu-dō; Asia; Japan; 日本国; Nihon-koku; Kansai region; 関西地方; Kansai-chihō; Kyoto Prefecture; 京都府; Kyōto-fu; Kyoto; Higashiyama-ku; 東山区; Places; " does not contain "日本国"
Previously I was using indexOf, which doesn't have this problem, but I can't use that since it doesn't match the whole word.
Is this a problem with my regex, is there a modifier I need to add to enable unicode support or something?
Thanks
Daveogre11 wrote:
> I need to use refind to deal with strings containing
accented characters like
> ?itt? l?su, but it doesn't seem to find them. Also when
using it with cyrillic
> characters , it won't find individual characters, but if
I test for [\w] it'll
> work.
works fine for me using unicode data:
<cfprocessingdirective pageencoding="utf-8">
<cfscript>
t="Tá mé in ann gloine a ithe;
Nà chuireann sé isteach nó amach
orm";
s="á";
writeoutput("search:=#t#<br>for:=#s#<br>found
at:=#reFind(s,t,1,false)#");
</cfscript>
what's the encoding for your data? -
Java Switch Statement with Strings
Apparently you cant make a switch statement with strings in java. What is the most efficient way to rewrite this code to make it function similar to a swtich statement with strings?
switch (type){
case "pounds":
type = "weight";
break;
case "ounces":
type = "weight";
break;
case "grams":
type = "weight";
break;
case "fluid ounces":
type = "liquid";
break;
case "liters":
type = "liquid";
break;
case "gallons":
type = "liquid";
break;
case "cups":
type = "liquid";
break;
case "teaspoons":
type = "liquid";
break;
case "tablespoons":
type = "liquid";
break;
}I'd create a Map somewhere with entries "liquid", "weight", etc.
public class Converter {
private static Map<String, List<String>> unitMap = new HashMap<String, List<String>>();
private static String[] LIQUID_UNITS = { "pints", "gallons", "quarts", "millilitres" };
private static String[] WEIGHT_UNITS = { "pounds", "ounces", "grams" };
static {
List<String> liquidUnits = new ArrayList<String>();
for (int i = 0; i < LIQUID_UNITS.length; i++) liquidUnits.add(LIQUID_UNITS));
unitMap.put("liquid", liquidUnits);
... // other unit types here
public String findUnitType(String unit) {
for (String unitType : unitMap.keySet()) {
List<String> unitList = unitMap.get(unitType);
if (unitList.contains(unit))
return unitType;
return null;
It might not be more "efficient", but it's certainly quite readable, and supports adding new units quite easily. And it's a lot shorter than the series of if/else-ifs that you would have to do otherwise. You'll probably want to include a distinction between imperial/metric units, but maybe you don't need it.
Brian -
How we can use OR operator with string in java??
Gaurav1 wrote:
its logical OR operator;how can we use it with matcher classLike its been said already. The "logical OR" is used the same everywhere.
Why don't you post the code you're having problems with. (Only the relevant areas, please. And use code tags.) -
Use listbox to work with strings
Hello guys,
I am struggling looking for a way to work with strings using a listbox. I think the problem is quite simple, but I don´t know how to implement it using labview.
I have an array of N elements (N is different everytime I run the program), and each element is a string (actually each element is a command). I would like load this array, and then if I select an element (by double-clicking or something similar), I would like to get a translation of the command.
In fact, I only need a way of loading the array, selecting an element, and then put it into a variable that would be translated with a SubVI that I have already implemented.
Do you have any clue of doing this? I have tried with the multicolumn listbox, but I cannot connect any array of strings to it, and I am starting to have a lack of new ideas...
Many thanks for your helpMaybe this helps you:
Norbert
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it. -
Revision: 3246
Author: [email protected]
Date: 2008-09-17 15:31:25 -0700 (Wed, 17 Sep 2008)
Log Message:
Fix fasttrack bug SDK-16910 - Simple List populated with strings throws RTE. This is fallout from the Group/DataGroup split. DefaultItemRenderer now uses a TextBox instead of a Group to show the list data.
QE: Any List tests that depended on the default item renderer to support anything other than text must be updated.
Bugs: SDK-16910
Ticket Links:
http://bugs.adobe.com/jira/browse/SDK-16910
http://bugs.adobe.com/jira/browse/SDK-16910
Modified Paths:
flex/sdk/trunk/frameworks/projects/flex4/src/flex/skin/DefaultItemRenderer.mxmlBTW, I do not experience the bug you had mentioned at
http://www.cs.rit.edu/~cxb0025/flex/TreeControlBugs.html
Can submit a video of my actions recorded -
Recursive WITH (Recursive Subquery Factoring) Never Returns
11.2.0.2 database on Windows, SQL Developer Version 3.2.20.09, build MAIN-09.87 (Database and SQL Developer are on the same machine. I have also tried connecting to a Linux 11.2 database and have the same results.)
I've been doing some simple testing with recursive WITH (Recursive Subquery Factoring) and when I run this following statement in SQL*Plus it returns instantly. However when running in SQL Developer it never returns, I've let it run for quite a long time (172 seconds) and gotten nothing, I finally kill the statement. Once I ran it and even killing the job didn't come back. I can get an explain plan but if I try to run it, run as script or autotrace it never returns. I have only one plan in the plan_table for this test, and it's only 4 lines long. No errors, no messages.
WITH get_plan (query_plan, id, planlevel) as
select ' '||operation||' '||options||' '||object_name query_plan, id, 1 planlevel
from plan_table
where id = 0
union all
select lpad(' ',2*planlevel)||p.operation||' '||p.options||' '||p.object_name query_plan, p.id, planlevel+1
from get_plan g, plan_table p
where g.id = p.parent_id
SELECT QUERY_PLAN FROM GET_PLAN ORDER BY PLANLEVEL;Hi Jeff, using either give the same results. The query is "running", as is the little graphic with the bouncing gray bar is moving back and forth saying either "Query Results" or "Scriptrunner Task" as appropriate.
OK this is odd. I run a count(*) on plan_table in SQL*Plus and get 4, in SQL Developer I get 487. Hun? That makes no sense I'm connect as the same user in each. Where are all these other entries coming from and why can't I see them in SQL Plus? Does SQL Developer have it's own PLAN_TABLE?
**EDIT --- Yes that seems to be the case. The PLAN_ID I see in SQL Plus doesn't even exist in the SQL Deveropler version of the table. OK that's good to know. I assume the plan_table for SQL Developer is local to it somehow? It's not in the database as best I can see.
Edited by: Ric Van Dyke on Feb 7, 2013 5:19 PM -
Problem with String to Int conversion
Dear Friends,
Problem with String to Int conversion
I am having a column where most of the values are numeric. Only 4 values are non numeric.
I have replaces those non numeric values to numeric in order to maintain the data type.
CASE Grade.Grade WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' ELSE Grade.Grade END
This comes the result as down
Grade
_0_
_1_
_10_
_11_
_12_
_13_
_14_
_15_
_16_
_17_
_18_
_19_
_2_
_20_
_21_
_22_
_23_
_24_
_3_
_4_
_5_
_6_
_7_
_8_
_9_
Refresh
Now I want to convert this value to numeric and do some calculation
So I changed the formula as below
cast (CASE Grade.Grade WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' ELSE Grade.Grade END as INT)
Now I get the following error
View Display Error
_ Odbc driver returned an error (SQLExecDirectW)._
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
_State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 17001] Oracle Error code: 1722, message: ORA-01722: invalid number at OCI call OCIStmtFetch. [nQSError: 17012] Bulk fetch failed. (HY000)_
SQL Issued: SELECT cast ( CASE Grade.Grade WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' ELSE Grade.Grade END as Int) saw0 FROM "Human Capital - Manpower Costing" WHERE LENGTH(CASE Grade.Grade WHEN 'E1' THEN '20' WHEN 'E2' THEN '21' WHEN 'E3' THEN '22' WHEN 'E4' THEN '23' ELSE Grade.Grade END) > 0 ORDER BY saw_0_
Refresh
Could anybody help me
Regards
Mustafa
Edited by: Musnet on Jun 29, 2010 5:42 AM
Edited by: Musnet on Jun 29, 2010 6:48 AMDear Kart,
This give me another hint, Yes you are right. There was one row which returns neither blank nor any value.
I have done the code like following and it works fine
Thanks again for your support
Regards
Code: cast (CASE (CASE WHEN Length(Grade.Grade)=0 THEN '--' ELSE Grade.Grade END) WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' when '--' then '-1' ELSE Grade.Grade END as Int) -
To use "analytic function" at "recursive with clause"
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2077142
The recursive member cannot contain any of the following elements:
・An aggregate function. However, analytic functions are permitted in the select list.
OK I will use analytic function at The recursive member :-)
SQL> select * from v$version;
BANNER
Oracle Database 11g Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> with rec(Val,TotalRecCnt) as(
2 select 1,1 from dual
3 union all
4 select Val+1,count(*) over()
5 from rec
6 where Val+1 <= 5)
7 select * from rec;
select * from rec
ERROR at line 7:
ORA-32486: unsupported operation in recursive branch of recursive WITH clauseWhy ORA-32486 happen ?:|Hi Aketi,
It works in 11.2.0.2, so it is probably a bug:
select * from v$version
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for IBM/AIX RISC System/6000: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
with rec(Val,TotalRecCnt) as(
select 1,1 from dual
union all
select Val+1,count(*) over()
from rec
where Val+1 <= 5)
select * from rec
VAL TOTALRECCNT
1 1
2 1
3 1
4 1
5 1 Regards,
Bob -
Let us discussion "non recursive with clause" usage
I think there are 3 "non recursive with clause" usage.
My question is do you know more "non recursive with clause" usage ?Another option is to use it to materialize remote data on the fly. Especially in combination with the materialize hint.
I think I used this tecnique once, but can't find the proper example anymore. Very simplified it could looked like this:
with fetchData as (Select /*+materialize */ * from myremoteTable@databaselink where status = 'CURRENT')
select *
from fetchdata r
full outer join localData l on r.id = r.id
where l.status = 'CURRENT'
;From 11g onwards: use the with clause to create better column names in larger select from dual combinations.
Not sure with that results in a suitable use case.
So instead of
with orders as
(select 1 id , 173 order#, 'John' customer, 'America' region from dual union all
select 2 id , 170 order#, 'Paul' customer, 'UK' region from dual union all
select 3 id , 240 order#, 'Hans' customer, 'Europe' region from dual union all
select 4 id , 241 order#, 'Francois' customer, 'Europe' region from dual )
select * from orders;you can now write
with
orders (id, order#, customer,region) as
(select 1 , 173 , 'John' , 'America' from dual union all
select 2 , 170 , 'Paul' , 'UK' from dual union all
select 3 , 240 , 'Hans' , 'Europe' from dual union all
select 4 , 241 , 'Francois' , 'Europe' from dual )
select * from orders;THis makes it a little easier to create tase data useing some excel sheet I guess. -
Hi All,
I am using oracle 11.2.0.4
I m using this for learning purpose
Below is my table and insert statement
CREATE TABLE NUMBERS(NUM NUMBER);
INSERT INTO NUMBERS VALUES(1);
INSERT INTO NUMBERS VALUES(2);
INSERT INTO NUMBERS VALUES(3);
INSERT INTO NUMBERS VALUES(4);
WITH RSFC(ITERATION,RUNNING_FACTORIAL) AS
(SELECT NUM AS ITERATION,
1 AS RUNNING_FACTORIAL
FROM NUMBERS
WHERE NUM=1
UNION ALL
SELECT R.ITERATION+1,
R.RUNNING_FACTORIAL * B.NUM
FROM RSFC R INNER JOIN NUMBERS B
ON (R.ITERATION+1) + B.NUM
SELECT ITERATION,RUNNING_FACTORIAL
FROM RSFC
I am learning recursive with clause
when I am trying to execute the query I am getting
ORA-00920 : invalid realtional operator
what is wrong in this query,please help me
Thanks and Regrds,
SubhoHi,
2937991 wrote:
Hi All,
I am using oracle 11.2.0.4
I m using this for learning purpose
Below is my table and insert statement
CREATE TABLE NUMBERS(NUM NUMBER);
INSERT INTO NUMBERS VALUES(1);
INSERT INTO NUMBERS VALUES(2);
INSERT INTO NUMBERS VALUES(3);
INSERT INTO NUMBERS VALUES(4);
WITH RSFC(ITERATION,RUNNING_FACTORIAL) AS
(SELECT NUM AS ITERATION,
1 AS RUNNING_FACTORIAL
FROM NUMBERS
WHERE NUM=1
UNION ALL
SELECT R.ITERATION+1,
R.RUNNING_FACTORIAL * B.NUM
FROM RSFC R INNER JOIN NUMBERS B
ON (R.ITERATION+1) + B.NUM
SELECT ITERATION,RUNNING_FACTORIAL
FROM RSFC
I am learning recursive with clause
when I am trying to execute the query I am getting
ORA-00920 : invalid realtional operator
what is wrong in this query,please help me
Thanks and Regrds,
Subho
The error actually has nothing to do with the WITH clause.
Join conditions (that is, the conditions following the ON keyword) must be expressions that evaluate to TRUE or FALSE. The join condition you posted, however
(R.ITERATION+1) + B.NUM
evaluates to a NUMBER. The following would be a valid join condition:
(R.ITERATION+1) = B.NUM
but I have no idea if that's what you wanted or not. -
Using intern() with String objects
Hi, I was wondering if anybody could explain to me more about using the method intern() with String objects.
Suppose I have a String, say testString1.
I want to make it uppercase and I would like to refer to it/use it in many places in the code.
Does using the statement
testString1.toUppercase().intern()
put the uppercase testString1 in some kind of a pool? so that everytime I call testString1.toUppercase(), a new testString1 in uppercase is not created each time? Instead it is taken from the pool? Is that how this works?
I would appreciate a response from anybody who knows about this stuff.
Thanks.No !!!... It doesn't work that way.
I would recommand you (and anyone alse) not to use String.intern() unless you have a COMPLETE understanding of how it works.
Maybe you are looking for
-
IPod 1st Gen (Touch wheel, 10GB) isn't recognized - missing software?
Recently, my 80GB video iPod decided to simply die on me, so I dug out my very first iPod, from 2001 or '02 I believe. It turns on fine, can scroll through menus, but there's nothing on it anymore - plug it into my computer (firewire-to-firewire), an
-
Dear all,I'm new in sapscript.now my user want me to change something to the INVOICE..In the Invoice..there is one SHIPPED FROM and TO: where should i check the coding ? the previously one is TO : Tokyo System Transport ..now the user wana change it
-
Gefen DVI to MiniDisplayPort woes
I recently bought a Gefen DVI to mini display port converter for use with my new 27" iMac. I connected the DVI output from my Powermac G5 (ATI X800 set at 1920x1200) to the Gefen box and then connected the Gefen to the iMac mini display port. I press
-
Hello All, I have one clarification.I have one field called amount.Its value is taken as 0.00 from the table. I have to make it as blank.I have made it as blank to that field in display pattern properties by setting as * in adobe designer. This is su
-
Safari is gone after software update
I have 4 users setup on my imac. Last evening I performed a software update per Apples suggestion...safari was one of the updates. Now today safari is gone. When any of the 4 users attemtps to select the icon, a grey question mark appears. If I go in